【问题标题】:Http file upload in asp.net mvc 2在asp.net mvc 2中上传Http文件
【发布时间】:2012-12-18 12:26:55
【问题描述】:

我有一个简单的 http 表单来使用 http 输入标签上传文件,如下所示:

<% using (Html.BeginForm("Add", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {%>
     <label for="file"> Select File :</label>
     <input type="file" name="uploadedFile" value="" accept="text/plain" class="fileUpload" />
     <input id="btnAdd" type="submit" class="button" name="Add" value=" Add "/>
<% } %>

回到控制器我有一个相应的动作:

[HttpPost]
public ActionResult Add(HttpPostedFileBase uploadedFile)
{
    Logger.Debug("Inside Add");

    ProductModel model = new ProductModel();

    if (uploadedFile.ContentLength > 0)
    {
        string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"),
                                       Path.GetFileName(uploadedFile.FileName));
        Logger.Debug("File saved at - " + filePath);
        uploadedFile.SaveAs(filePath);
        model.FileName = uploadedFile.FileName;
        model.Add(filePath);
        return View("Result", model);
    }
    return RedirectToAction("Index");
}

在我的开发机器和实验室服务器上,这可以完美运行。但在生产中,它甚至不会发送任何提交按钮的请求。我通过提琴手检查了请求,它没有显示任何请求甚至来自此表单的任何痕迹,来自 win 2008 r2 生产服务器上的 IE 8。

我在这里一无所知。可能是什么问题?您认为 IE 安全权限、UAC、组策略可能是个问题吗?或者你还有话要说。

【问题讨论】:

  • 试过其他浏览器比如chrome,看看控制台日志信息?
  • 不。好点子。但是提琴手给出了相同的效果。它没有显示浏览器和 IIS 之间发生的任何通信
  • 我怀疑可能有一些 javascript 错误阻止表单提交
  • 我同意,我的猜测是有一些不明显的 javascript 验证。

标签: asp.net asp.net-mvc http asp.net-mvc-2 file-upload


【解决方案1】:

尝试在视图中将文件输入的名称从uploadFile 更改为UploadedFile

<% using (Html.BeginForm("Add", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {%>
     <label for="file"> Select File :</label>
     <input type="file" name="UploadedFile" value="" accept="text/plain" class="fileUpload" />
     <input id="btnAdd" type="submit" class="button" name="Add" value=" Add "/>
<% } %>

【讨论】:

  • 那是我所做的重构,之前在两个地方都是“uploadFile”。这不会改变任何事情。
猜你喜欢
  • 2023-04-03
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
相关资源
最近更新 更多