Upload image to server using C#/.NET and storing filename in DB
To store the file in an images folder, it should be:
FileUpload1.SaveAs(Server.MapPath("~/Images/" + FileUpload1.FileName));
and then add the command parameters in the fileName
comm.Parameters["@FileName"].Value = FileUpload1.FileName;
Note: you must have the FileName
field in your DB table.
michaelmcgurk
BY DAY: Alt-Rock Ninja Cowgirl at Veridian Dynamics. BY NIGHT: I write code and code rights for penalcoders.example.org, an awesome non-profit that will totally take your money at that link. My kids are cuter than yours. FOR FUN: C+ Jokes, Segway Roller Derby, NYT Sat. Crosswords (in Sharpie!), Ostrich Grooming. "If you see scary things, look for the helpers-you'll always see people helping."-Fred Rogers
Updated on June 25, 2022Comments
-
michaelmcgurk almost 2 years
I'm currently using the following snippet to insert data into a table in my database. It works great. But, I want to start adding filename data and not sure how to proceed.
I have the following:
// Create command comm = new SqlCommand( "INSERT INTO Entries (Title, Description) " + "VALUES (@Title, @Description)", conn); // Add command parameters comm.Parameters.Add("@Description", System.Data.SqlDbType.Text); comm.Parameters["@Description"].Value = descriptionTextBox.Text; comm.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar, 50); comm.Parameters["@Title"].Value = titleTextBox.Text;
I also have a File Upload option. But, I don't know how to use this to do the following:
- move the file to my images directory and
- store the filename value in my table.
I have added the correct enctype to my form but now a little lost.
Can someone explain the best way to do this?
Many thanks for any help with this.
-
michaelmcgurk almost 13 yearsBrilliant!!! This looks good to me. I'm not near the code right now but will be later today. I will let you know how I get on. Many thanks :D
-
michaelmcgurk almost 13 yearsGood shout. I'm normally a bit weary of adding file data straight into the database but will take a look at this. Thanks for such a prompt reply.
-
Arunas almost 13 yearsWhat happens when you try to upload another file with the same file name? :) Should rather store original file name in DB but use something like a random guid or content hash for storing it on server filesystem.
-
Muhammad Akhtar almost 13 yearsyes, it would be better if you store original file name in the DB and store it file system with GUID.
-
michaelmcgurk almost 13 yearsHi. I'm trying this just now but unfortunately get the error
"CS1061: 'System.Web.UI.HtmlControls.HtmlInputFile' does not contain a definition for 'SaveAs' and no extension method 'SaveAs' accepting a first argument of type 'System.Web.UI.HtmlControls.HtmlInputFile' could be found (are you missing a using directive or an assembly reference?)
Any ideas? -
michaelmcgurk almost 13 yearsMy Form is:
<form runat="server" enctype="multipart/form-data">
and has<input id="filUpload" type="file" name="filUpload" runat="server">
. My codebehind file includes:filUpload.SaveAs(Server.MapPath("~/Images/" + filUpload.FileName));
andcomm.Parameters["@FileName"].Value = filUpload.FileName;
-- Hope this is useful. Many thanks. -
Muhammad Akhtar almost 13 yearsIf you are using HTML file upload control, then it should be like...File1.PostedFile.SaveAs and if you are using asp.net file upload control <asp:FileUpload ID="FileUpload1" runat="server" /> then in FileUpload1.SaveAs
-
michaelmcgurk almost 13 yearsIT WORKED!!!! WOO-HOOOO. Muhammad. You are a legend. High five from Scotland :D Thanks so much.
-
michaelmcgurk almost 13 yearsFinal wee issue I'm having.... using:
comm.Parameters["@FileName"].Value = filUpload.FileName;
gives me the errorSystem.IndexOutOfRangeException: An SqlParameter with ParameterName '@FileName' is not contained by this SqlParameterCollection.
-- Any ideas how to fix this? Many thanks once more. -
Muhammad Akhtar almost 13 yearsYou need to update your insert query and have to add parameter in your insert query...new SqlCommand( "INSERT INTO Entries (Title, Description,FileName) " + "VALUES (@Title, @Description,@FileName)", conn);
-
michaelmcgurk almost 13 yearsThanks for your patience with me :) My new INSERT query is
comm = new SqlCommand("INSERT INTO news (title, body, date_added, img1sml) VALUES (@Title, @Description, @DatePosted, @img1sml)", conn);
Currently I'm adding data like this:comm.Parameters.Add("@DatePosted", System.Data.SqlDbType.Date); comm.Parameters["@DatePosted"].Value = DateTime.Now.ToString();/*error on the img1sml Add line*/comm.Parameters.Add["@img1sml"].Value = filUpload.FileName;comm.Parameters["@img1sml"].Value = filUpload.FileName;
The DatedPosted works great, but not my file name. Thanks again -
michaelmcgurk almost 13 yearsIt seems this line is incorrect:
comm.Parameters.Add["@img1sml"].Value = filUpload.FileName;
. Thanks once more. I'm now "approving" past comments. -
michaelmcgurk almost 13 yearsHi Muhammad. Sorry to be a pest, but here is my current code: pastebin.com/SHpUHfsE. Using this, I get the error:
Exception Details: System.IndexOutOfRangeException: An SqlParameter with ParameterName '@img1sml' is not contained by this SqlParameterCollection.
Do I need to "Add" @img1sml to my parameter list? Thank you -
michaelmcgurk almost 13 yearsResult! I used
comm.Parameters.Add("@img1sml", System.Data.SqlDbType.Text); comm.Parameters["@img1sml"].Value = filUpload.FileName;
and it now works. Many thanks again Muhammad.