Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi ,
I am trying to upload image to server but getting error not a valid virtual path
i want to upload image to media folder by fileuploader control .
please guide.
Quote:
string a = HttpContext.Current.Server.MapPath("http://printofast.com/media/");
string fileName = System.IO.Path.Combine(Server.MapPath(a, fileuploadPimage.FileName);
fileuploadPimage.SaveAs(fileName);


What I have tried:

string a = HttpContext.Current.Server.MapPath("http://printofast.com/media/");
string fileName = System.IO.Path.Combine(Server.MapPath(a, fileuploadPimage.FileName);
fileuploadPimage.SaveAs(fileName);

---
// fileuploadPimage.SaveAs(Config.GetConfigValueAsString("ProductImagesPath") + strfilename);
//fileuploadPimage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["ImagePath"] ,strfilename));
Posted
Updated 23-Jun-16 0:16am

Server.Mappath converts a virtual path like "~\resources\images\myPicture.jpg" into a "proper" qualified path that the file system on the server running IIS understands - where "~" is the root folder for your website. You can't feed it a URL and expect it to translate that into a file system address.
If you are trying to save a file into your website, then try
C#
fileupload.SaveAs(Path.Combine(Server.MapPath("~/Media"), fileuploadPimage.FileName);

But be aware that it's very common for different users to have the same name for different files. I'd use a DB to store the filename, the user that uploaded it, and a Guid ID column, and save the file to the disk using the Guid value as the actual filename. That way, different users can safely have files with the same name and they don't interfere with each other.
 
Share this answer
 
Try this to insert the image to folder and save the file path to database.

C#
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("~/Imgdata/" + filename));
        SqlCommand cmd = new SqlCommand("Insert into Image_data(ImgTitle,Imgdata)values(@ImgTitle,@Imgdata)",con);
        con.Open();
        cmd.Parameters.AddWithValue("@ImgTitle", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Imgdata", "/imgdata/" + filename);
        cmd.ExecuteNonQuery();
        lblerror.ForeColor = Color.Green;
        lblerror.Text = "Insert Success";
        con.Close();
 
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