Link to home
Start Free TrialLog in
Avatar of gianitoo
gianitoo

asked on

upload file to folder and insert to db

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>
Avatar of Ehrick
Ehrick

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**
Avatar of gianitoo

ASKER

can u send it in vb.net
ASKER CERTIFIED SOLUTION
Avatar of Ehrick
Ehrick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial