ASP.NET MVC3 文件上传不工作

标签 asp.net asp.net-mvc-3 file-upload

页面代码

<% using (Html.BeginForm())
   { %>
<fieldset>
    <legend>上传项目材料</legend>
    <input  type="file" name="File1" />
    <input  type="submit" value="上传" />
</fieldset>
<%} %>

Action 代码

[HttpPost]
public ActionResult FileUpLoad(int id, FormCollection form)
{
    try
    {
        var model = db.ProjcetDeclare.First(c => c.id == id);

        if (Request.Files.Count==0)
        {
            return View();
        }
        string newFile=string.Empty;

        var File1 = Request.Files[0];
        if (File1.ContentLength == 0)
        {
        }
        newFile = model.Project.pname + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(File1.FileName);
        File1.SaveAs(Server.MapPath("/项目材料/" + newFile));

        model.XMCL = "/项目材料/" + newFile;
        UpdateModel(model);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

我在努力 但是 Request.Files.Count==0 是真的 找不到文件 为什么?

最佳答案

本周早些时候刚遇到这个问题。您的表单标记缺少 HTML 属性 (enctype) 来告诉服务器它正在发布文件。这是解决方案...

using (Html.BeginForm("Index", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))

关于ASP.NET MVC3 文件上传不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4766281/

相关文章:

rest - 为什么我的上传文件 POST 不能使用 NEST.JS 和 Multer?

javascript - 检查文件名中的无效字符(特殊字符和国际字符)而不验证文件路径

c# - ASP.Net 版本/内部版本号

asp.net - ASP.NET-错误/异常处理程序(寻找某些项目)

c# - 无法找到或加载程序集“tmpAssembly,

c# - 如何实现模型以强制客户端至少填写 ASP.NET MVC 中的两个输入之一

php - 如何使用 file_get_contents ('php://input' 获取文件信息 + 正文)?

c# - 从自定义 httpHandlers 评估 ASPX 页面

.net - 使用 log4net 创建记录器库的最佳实践。是

c# - 使用 JS 函数调用的 MVC3 中的分页问题