开发者

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,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜