Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

upload file to folder and insert to  db

Posted on 2007-03-21
3
Medium Priority
?
513 Views
Last Modified: 2008-01-09
how do you upload a file into a folder called images   and also save the file name into a column in sql server 2005.  i have my code below.  feel free to tweak it if needed

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:JMIConnectionString %>"
                       InsertCommand="INSERT INTO [Clientbrief] ([reserach]) VALUES (@reserach)"
   
                                   
                       <InsertParameters>
       
                           <asp:FormParameter FormField="Reserach" Name="Reserach" Type="String"
                               />
                   Type="String" />
                       </InsertParameters>
                   </asp:SqlDataSource>
0
Comment
Question by:gianitoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:Ehrick
ID: 18766290
Assuming that you are uploading a file selected by a user from an asp.net 2 page, you'd do the following:

add a FileUpload control to your aspx page.
on Submit, get the file and read it as a ByteStream into a Bitmap or Image object.
Write that Bitmap to your Images folder, being sure to add the appropriate image flag (jpeg, bmp, gif, etc.). Finally send that file name to your sqldatasource.

The sample code is from one of my pages.
Example Code (in your aspx file):
    <asp:FileUpload ID="photoFile1" runat="server" />

(in your code behind file):
HttpPostedFile uploadedImageFile = Request.Files[0];
            if (uploadedImageFile.ContentLength>0)
            {
                if (System.IO.Path.GetExtension(uploadedImageFile.FileName).ToLower() != ".jpg" &&
                    System.IO.Path.GetExtension(uploadedImageFile.FileName).ToLower() != ".gif")
                {
                    lblPhotoErrors.Text = "One or more files that you selected are not JPEG or GIF format.";
                    return;
                }

                //**Get the image**
                try
                {
                    Bitmap uploadedPhoto = new Bitmap(uploadedImageFile.InputStream);

                    //save the image with a unique file name
                    string uploadFileName = System.IO.Path.GetFileNameWithoutExtension(uploadedImageFile.FileName) + ".jpg";
                    int fileAppendNum = 0;
                    while (System.IO.File.Exists(Server.MapPath("Images/" + uploadFileName)))
                    {
                        fileAppendNum++;
                        uploadFileName = System.IO.Path.GetFileNameWithoutExtension(uploadedImageFile.FileName) +
                            fileAppendNum.ToString() + ".jpg";
                    }
                   
                    uploadedPhoto.Save(Server.MapPath("uploadedImages/" + uploadFileName),
                        ImageFormat.Jpeg);
                   
                    //TODO: Write uploadFileName to your sql database

                    //Destroy objects. This may not be necessary.
                    uploadedPhoto.Dispose();
                }
                catch (ArgumentException errArgument)
                {
                    //The file was not a valid jpg file
                    lblPhotoErrors.Text = "One or more invalid files. (Not a jpg.)";
                        System.IO.File.Delete(Server.MapPath("uploadedImages/" + fileName));
                }
                //**End image part**
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18766457
can u send it in vb.net
0
 
LVL 2

Accepted Solution

by:
Ehrick earned 2000 total points
ID: 18766848
I'm not really familiar with vb.net. However, another expert may be able to do the translation, or you may be able to find appropriate substitutes yourself. I put the code into a c# to vb.net translator at http://authors.aspalliance.com/aldotnet/examples/translate.aspx. You might try this and see if it works:

Dim uploadedImageFile As HttpPostedFile = Request.Files(0)
If uploadedImageFile.ContentLength > 0 Then
   If System.IO.Path.GetExtension(uploadedImageFile.FileName).ToLower() <> ".jpg" And System.IO.Path.GetExtension(uploadedImageFile.FileName).ToLower() <> ".gif" Then
      lblPhotoErrors.Text = "One or more files that you selected are not JPEG or GIF format."
      Return
   End If
   
   '**Get the image**
   Try
      Dim uploadedPhoto As New Bitmap(uploadedImageFile.InputStream)
     
      'save the image with a unique file name
      Dim uploadFileName As String = System.IO.Path.GetFileNameWithoutExtension(uploadedImageFile.FileName) + ".jpg"
      Dim fileAppendNum As Integer = 0
      While System.IO.File.Exists(Server.MapPath(("Images/" + uploadFileName)))
         fileAppendNum += 1
         uploadFileName = System.IO.Path.GetFileNameWithoutExtension(uploadedImageFile.FileName) + fileAppendNum.ToString() + ".jpg"
      End While
     
      uploadedPhoto.Save(Server.MapPath(("uploadedImages/" + uploadFileName)), ImageFormat.Jpeg)
     
      'TODO: Write uploadFileName to your sql database
      'Destroy objects. This may not be necessary.
      uploadedPhoto.Dispose()
   Catch errArgument As ArgumentException
      'The file was not a valid jpg file
      lblPhotoErrors.Text = "One or more invalid files. (Not a jpg.)"
      System.IO.File.Delete(Server.MapPath(("uploadedImages/" + fileName)))
   End Try
End If '**End image part**
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question