MVC 5中的上传;t使用服务器,但在与
本文关键字:服务器 MVC | 更新日期: 2025-02-19 10:39:29
我正在尝试将文件上传到服务器,我的网站是用MVC 5创建的。当我在电脑上使用visualstudio时,所有数据库和foot文件夹都是成功上传的本地文件,但当我尝试上传到服务器时,文件不会上传。
我的观点是:
<form action="UploadFile" method="post" enctype="multipart/form-data">
<label>Upload File:</label>
<input type="file" name="file" id="file" />
<input type="submit" id="submitbtn" class="btn btn-default" />
@ViewBag.Path
</form>
控制器是:
public ActionResult UploadFile(HttpPostedFileBase file)
{
System.Web.HttpContext.Current.Session["path"] = "";
if (file == null )
return RedirectToAction("Create");
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/attachments/"), fileName);
file.SaveAs(path);
pathFile = path;
ViewBag.Path = path;
}
return View("Create");
}
MVC 5中的Upload不适用于服务器,但在VS中可以在本地工作。我的代码是正确的,因为它在本地工作,我和服务器的连接也是正确的,我通过web在服务器内的数据库中编辑和创建记录。
非常感谢我基于freinds指南的问题已经解决,问题是附件文件夹的安全性。我在安全选项卡中授予IIS_IUsrs对附件文件夹的权限,所以代码在服务器中工作最好的花园tomas