简体   繁体   English

在ASP.NET MVC 2中上传Http文件

[英]Http file upload in asp.net mvc 2

I have simple http form to upload file using http input tag as below: 我有一个简单的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 "/>
<% } %>

and back in controller I have a corresponding Action: 然后回到控制器,我有一个对应的动作:

[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");
}

On my development machine and on lab server, this works perfectly. 在我的开发机器和实验室服务器上,这非常有效。 But on production it does not even send any request for submit button. 但是在生产中,它甚至不发送任何提交按钮的请求。 I inspected requests through fiddler, it does not show any traces that request is even originating for this form, from IE 8 on win 2008 r2 production server. 我通过提琴手检查了请求,它没有显示该请求甚至来自此表格的任何痕迹,这些痕迹来自Win 2008 R2生产服务器上的IE 8。

I'm clueless here. 我在这里一无所知。 What could be the issue? 可能是什么问题? Do you think IE security permissions, UAC, group policies might be an issue? 您认为IE安全权限,UAC,组策略可能是一个问题吗? or you have something else to say. 或者您还有其他话要说。

try changing the name of the file input from uploadFile to UploadedFile in the View 尝试在视图中将输入的文件名从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 "/>
<% } %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM