Solved

upload file to folder and insert to  db

Posted on 2007-03-21
3
499 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now