微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 在MVC文件上传中Request.files为空

我有同样的问题
@using (Html.BeginForm("CreateRequest","SupportRequest",FormMethod.Post,new { id = "frmstemplate",enctype = "multipart/form-data" }))
{
  <td><input type="file" name="FirstFile" id="FirstFile" class="button"  /> 
  <input  type="button" class="button" id="FirstFileupload"  value="upload" onclick="Javascript:DocumentUpload();"/>
}

<script language="javascript" type="text/javascript">
    function DocumentUpload()
    {
        var browseFile = $('#FirstFile').val();

        if (browseFile != null && browseFile != "") {
            alert(browseFile);
            $.ajax({
                type: 'POST',dataType: 'json',url: '@Url.Content("~/SupportRequest/UploadFiles")?fileElementId=' + browseFile,success: function (data) {
                    alert('Hi'); //debugger;
                    if (data.Result == "SUCCESS") {
                        alert('Hi');
                    }
                    else {
                        ShowInfo('Document Uploaded Successfully');
                    }
                }
            });
        }
    }
</script>

在控制器方面,我有

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadFiles(string fileElementId,FormCollection formColl)
{
    var FirstFile = Request.Files;
    foreach (string upload in Request.Files)
    {
        if (!Request.Files[upload].HasFile()) continue;
        string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
        string filename = Path.GetFileName(Request.Files[upload].FileName);
        Request.Files[upload].SaveAs(Path.Combine(path,filename));
    }
    return Json(new { Filename = "" });
}

但是我的Request.Files总是为空.

我尝试了几件事,比如将代码更改为Request.Files [“FirstFile”]等.每次,文件集合都是空的.

解决方法

您需要在控制器操作参数中使用HttpPostedFileBase才能获取已发布的filedata.

请阅读Phil Haack的完整article

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐