简体   繁体   中英

Server.mappath confusion

There is a little confusion in my mind about server.mappath which is correct and what's the difference betwwen these two

FileUpload1.saveAs(Server.MapPath("~/User/images/")+"ankush.jpg"));

FileUpload1.saveAs(Server.MapPath("~/User/images")+"ankush.jpg"));

The correct way of using MapPath() would be:

FileUpload1.saveAs(Server.MapPath("~/User/images/ankush.jpg"));

or if you insist:

FileUpload1.saveAs(Path.Combine(Server.MapPath("~/User/images"),"ankush.jpg")));

MapPath() doesn't append a trailing backslash to the mapped path because it has no way of knowing if the path is a directory or a file (it doesn't check if the given path actually exists)

I would advise you to use this way

FileUpload1.saveAs(Server.MapPath("~/User/images/ankush.jpg"));

Reason : because if you already know the path then why break down the filename separately If the filename was getting passed by parameter then you could do

FileUpload1.saveAs(Server.MapPath(String.Format("~/User/images/{0}", fileName)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM