Click here to Skip to main content
15,936,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to save image physical path in database and upload image to the disired folder in my project using file upload control..

i am new in uploading images so don't know exactly how to do that..

i tried to do that using this method

C#
if (FileUpload1.HasFile)
        {
            string path = Server.MapPath(".") + "\\Images\\" + FileUpload1.FileName;
            FileUpload1.SaveAs(path);

         
            string query = "insert into Products(CategoryID,ProductName,ProductImage,UnitCost,Description)select CategoryID,'" + txtproname.Text + "','" + FileUpload1.FileName + "'," + txtproprice.Text + ",'" + txtprodesc.Text + "' from Categories where CategoryName='" + DropDownList1.Text + "'";


i am not exactly understanding the Server.MathPath. but its uploading the images in the admin folder where my page products.aspx (from which i am uploading images ) is present. moreover its save just the filename like
C#
product.gif
(not the ful path.)

i want to save image in the root images folder. can anyone pls help me to do that. and i want to save complete physical path in database as
~/images/product.gif
Posted
Comments
AditSheth 12-Sep-11 8:34am    
Instead of "." in server.MapPath use "~" to locate root folder
"." - Current Page Folder Location
"~" - Project Folder Location

If the path you pass to MapPath does not start with either a forward or back slash the method returns a path relative to the directory the page is in. Which is what you are seeing when images are uploaded in the admin folder. The following should produce what you want.
C#
string path = Server.MapPath(@"/Images") + FileUpload1.FileName;


As for the DB issue it is only inserting product.gif since you are just passing in FileUpload1.FileName which contains just the filename. If you want the full path you need to pass in the path variable. You should really get out of the bad habit of writing you SQL statements using string concatenation. There are no pros to using string concatenation, and plenty of cons.
 
Share this answer
 
v2
If you want to save rooted path like Path ="Images\product.gif"
C#
if (FileUpload1.HasFile)
{
    string path =  @"Images\" + FileUpload1.FileName;
    FileUpload1.SaveAs(Server.MapPath("~") + @"\" + path);
    // Save process strfilename = path 
}
 
Share this answer
 
XML
hi,

try to give following format.,


<code>if (FileUpload1.postedfile.contentlength>0)
{
  string path = Server.Mappath(@"~/images/"+'"+FileUpload1.Filename+"')
  FileUpload1.SaveAs(path);

//now save the path value to your database.

   string query = "insert into Products(CategoryID,ProductName,ProductImage,UnitCost,Description)select CategoryID,'" + txtproname.Text + "','" + path  + "'," + txtproprice.Text + ",'" + txtprodesc.Text + "' from Categories where CategoryName='" + DropDownList1.Text + "'";
}
else
{
messagebox("file content length is zero");
}</code>
i hope this is useful to you...
take care
 
Share this answer
 
v3
try this

C#
public const string FOLDER_UPLOADIMAGE_PATH = ".\\UploadedImages\\";
HttpPostedFile file = FUProductImage.PostedFile;
            string strfilename = FUProductImage.PostedFile.FileName;
            string c = System.IO.Path.GetFileName(strfilename);
            FUProductImage.PostedFile.SaveAs(Server.MapPath(FOLDER_UPLOADIMAGE_PATH + c));
            string filepath = Server.MapPath(FOLDER_UPLOADIMAGE_PATH + c);
 
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