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

前端之家收集整理的这篇文章主要介绍了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

猜你在找的asp.Net相关文章