Solved

upload file to folder and insert to  db

Posted on 2007-03-21
3
503 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
  • 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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