Click here to Skip to main content
16,015,046 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
SQL
Hi,
From my asp.net form, I am trying to save a picture to my SQL Database.  Problem is the File Upload button just takes the name of the file, not the full path.  When I try to save it, it says file not found.
Is there something like OpenFileDialog in webforms, as I use that for vb.net and that works excellent.

Thanks
Posted

That's because the FileUpload control does not save the file to disk - it doesn't know what you want to do with it.
Here is the code I use:

C#
/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            int version = 0;
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            string strCon = ConnectionStrings.Download;
            using (SqlConnection con = new SqlConnection(strCon))
                {
                con.Open();
                // Stick file into database.
                using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
                                                       "VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
                    {
                    ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
                    ins.Parameters.AddWithValue("@FN", filename);
                    ins.Parameters.AddWithValue("@DS", "");
                    ins.Parameters.AddWithValue("@DT", filedata);
                    ins.Parameters.AddWithValue("@VS", version);
                    ins.Parameters.AddWithValue("@UD", DateTime.Now);
                    ins.ExecuteNonQuery();
                    }
                }
            return string.Format("{0} uploaded, version = {1}", filename, version);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }

That gets called from the Upload Button Click event handler.
 
Share this answer
 
hy this is acode where i stored image in folder..
and i also recmmd u to use folder option becoz i u store image in database ..then it tak spac


static string ext;
public void btnUpload_Click(object sender, System.EventArgs e)
{
HttpPostedFile myFile = filUpload.PostedFile;
if (myFile != null && myFile.ContentLength != 0 && myFile.FileName!= "")
{
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".yuv" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".tif" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".thm" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".pspimage" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".psd" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".png" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".gif" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".bmp")
{
Response.Write("<Script>alert('Please Select Only Image File')</Script>");
imgPicture.ImageUrl = "";
}
else
{
if (TxtRef.Value != "" || TxtRef.Value.Length != 0)
{
ext = System.IO.Path.GetExtension(this.filUpload.PostedFile.FileName);
string Id = TxtRef.Value+ext;
myFile.SaveAs(Server.MapPath(@"~/images/" +Id));
imgPicture.ImageUrl = "~/images/" + Id;

conn.Open();
SqlCommand updateimage = new SqlCommand("update ack set picsize='"+ext+"' where RefNo='"+TxtRef.Value+"'",conn);
updateimage.ExecuteNonQuery();
conn.Close();
}
else
{
Response.Write("<Script>alert('Please Select Your Ref N0. firstly....')</Script>");
imgPicture.ImageUrl = "";
}
}
}
}


and if u r nt satisfy then i will send u that exmple also
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900