Solved

upload file to folder and insert to  db

Posted on 2007-03-21
3
497 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
Comment Utility
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
Comment Utility
can u send it in vb.net
0
 
LVL 2

Accepted Solution

by:
Ehrick earned 500 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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.…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

10 Experts available now in Live!

Get 1:1 Help Now