Errors on uploading file images to server
I want to upload file images to server but I get errors. I can't identify the problem
I use this line of code to upload using asp.net fileupload
control
FileUpload1.PostedFile.SaveAs(Server.MapPath("products_images/")
开发者_Go百科 + FileUpload1.PostedFile.FileName);
It happens that PostedFile.FileName has the entire file path: "C:\blabla\files\img.jpg" This is not a valid path to your webApplication, mainly after you concat it with products_images/ --> "products_images/C:\blabla\files\img.jpg". That is why you get your exception.
Try this simple filename treatment:
int lastSlash = FileUpload1.PostedFile.FileName.LastIndexOf("\\");
string treatedName = FileUpload1.PostedFile.FileName.Substring(lastSlash);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/products_images/") + treatedName);
Regards,
精彩评论