【发布时间】:2014-04-03 00:16:30
【问题描述】:
我可以使用此代码成功上传文件:
[HttpPost]
public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments)
{
// The Name of the Upload component is "attachments"
foreach (var file in attachments)
{
// Some browsers send file names with full path. This needs to be stripped.
var fileName = Path.GetFileName(file.FileName);
var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
file.SaveAs(physicalPath);
}
// Return an empty string to signify success
return Content("");
}
但这会将文件放在正在运行的应用程序 App_Data 文件夹中。我想将文件放在我的局域网上的共享中。我已经尝试过下面的代码,但它总是将 myPath 附加到应用程序的路径中:
[HttpPost]
public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments)
{
// The Name of the Upload component is "attachments"
foreach (var file in attachments)
{
// Some browsers send file names with full path. This needs to be stripped.
var fileName = Path.GetFileName(file.FileName);
//add possible new folder name to path
var myPath = "//my-server-myshare/myfolder/";
//combine with file name
var physicalPath = Path.Combine(Server.MapPath(myPath), fileName);
file.SaveAs(physicalPath);
}
// Return an empty string to signify success
return Content("");
}
如何强制将其保存在我作为 myPath 的 URL 中?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 file-upload