我可以使用以下代码成功上传文件:
[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中?
网络共享必须是完整的UNC路径,因此请绕开斜线:
var myPath = @"\\my-server-myshare\myfolder\";
您也可以直接创建文件:
file.Create(@"\\my-server-myshare\myfolder\");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句