【问题标题】:Handling input type file in asp net mvc在 asp net mvc 中处理输入类型文件
【发布时间】:2013-01-29 19:41:12
【问题描述】:

我进行了深入研究,试图找到从客户端上传文件的更实用的方法,我决定使用输入类型文件 html 元素。

问题是: 我在控制器中什么都没有,我什至看不到在 firebug 中设置了 fakepath 字符串。

查看:

<form action="action" id="id" method="POST" enctype="multipart/form-data">
    <table id="tblId">            
        <tr>
            <td><input type="file" name="file" /></td>
        </tr>
    </table>
    <input type="submit" value="import" />
</form>   

如您所见,我正确使用了 enctype。数据已按应有的方式发送到控制器,但没有数据。

控制器:

[HttpPost]
public ActionResult opImportFile(FormCollection form) {


    var file = Request.Files["file"];
        if (file != null)
        {
            return Content("ok");
        }
        else
        {
            return Content("bad");
        }
}

而且我总是变得“坏”!太糟糕了。 有没有其他方法可以尝试或者我做错了什么?

P.S -> 这是一个ajax请求:

$('#formUpdStoringSettings').submit(function (e) {

    e.preventDefault();    
    var form = $(this);       
    alert($('input[name=file]').val()); //Here I am able to get the fakepath...
    alert(form.serialize());  //Here I get nothing...

    $.ajax({
        url: form.attr('action'),
        type: form.attr('method'),
        data: form.serialize(),
        success: function (response) {
        }
   });
}

【问题讨论】:

标签: c# asp.net .net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您可能需要使用jQuery upload plug-in 才能使用 AJAX 进行上传。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-06-22
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多