Why can't my code save files to C drive?

2019-09-10 10:36发布

问题:

I am saving my image to a folder SiteImages that resides within project root but I want to place it on my C drive and want to save and access pictures from there i.e. C;/SiteImages but I can't figure out

Code: (Upload)

string fileName = Path.GetFileName(FileUpload1.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/SiteImages/") + fileName);

Code: (Access)

string FilePath = new Uri(Server.MapPath("~/SiteImages/")).AbsoluteUri;

please help to make it work within C drive

UPdate: problem is with gridview picking picture from C drive now.

Old:

<img src='../SiteImages/<%# Eval("PersonalInfoEmployeePicture") %>' width="20" height="20" onmouseover="ShowFull(this)" onmouseout="ShowActual(this)" />

回答1:

It should be as simple as it is, right?

string fileName = Path.GetFileName(FileUpload1.FileName);
FileUpload1.PostedFile.SaveAs("C:\\SiteImages\\" + fileName);

string FilePath = "C:\\SiteImages\\" + fileName;

UPDATED: As you said that you want it to be accessed as absolute URI, create a Virtual Directory in IIS pointing to SiteImages folder in C:. With that you can use Server.MapPath function to map the path into the folder you specified.



回答2:

just remove the begin .. in image source

<img src='/SiteImages/<%# Eval("PersonalInfoEmployeePicture") %>' width="20" height="20" onmouseover="ShowFull(this)" onmouseout="ShowActual(this)" />

If you still using below code to save uploaded images

FileUpload1.PostedFile.SaveAs(Server.MapPath("~/SiteImages/") + fileName);