简体   繁体   English

Server.mappath混乱

[英]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 我对server.mappath的想法有点困惑,这是正确的,这两个之间有什么区别

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

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

The correct way of using MapPath() would be: 使用MapPath()的正确方法是:

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) MapPath()不会在映射的路径后面附加反斜杠,因为它无法知道该路径是目录还是文件(它不会检查给定的路径是否实际存在)

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)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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