简体   繁体   English

MVC 4 Ajax异步文件上传始终将空值传递给控制器

[英]MVC 4 Ajax Asynchronous File Upload Always Passing Null Value To Controller

I'm having a problem when trying to upload file asynchronously from ajax to my controller. 尝试从ajax异步上传文件到我的控制器时遇到问题。 I have 3 variables to pass (PictureId, PictureName, and PictureFile) . 我有3个要传递的变量(PictureId,PictureName和PictureFile) The problem lies on my "PictureFile" variable which should be holding the uploaded file itself but instead it always pass null value to the controller. 问题出在我的“ PictureFile”变量上,该变量应该保存上载的文件本身,但始终将空传递给控制器。

Here is my view model 这是我的视图模型

public class PictureUpload
{
    public int PictureId { get; set; }
    public string PictureName { get; set; }
    public HttpPostedFileBase PictureFile { get; set; }
}

Here is my controller 这是我的控制器

public JsonResult EditPicture(PictureUpload picture)
{
    //do something here
}

Here is my form 这是我的表格

<div class="thumbnail" id="uploadForm">
    <form name="uploadForm" enctype="multipart/form-data">
        <input type="text" name="fileId" hidden="hidden" />
        <input type="file" name="file" style="max-width: 100%" />
        <input type="button" name="cancel" value="cancel" />
        <span>
            <input type="button" name="upload" value="upload" />
        </span>
    </form>
</div> 

Here is my script 这是我的剧本

$("input[name=upload]").click(function () {
    var $pictureId = $("input[name=fileId]").val();
    var $pictureFile = $("input[name=file]").get(0).files[0];

    $.ajax({
        type: 'POST',
        url: '@Url.Action("EditPicture", "Restaurant")',
        contentType: "application/json",
        dataType: 'json',
        data: JSON.stringify({ PictureId: $pictureId, PictureName: $pictureFile.name, PictureFile: $pictureFile }),
    });

In my controller parameter. 在我的控制器参数中。 "PictureId" and "PictureName" holds the correct value, but "PictureFile" is always null. “ PictureId”“ PictureName”保留正确的值,但“ PictureFile”始终为空。 So it means something is going just not right when the system passes the parameter from ajax to controller. 因此,这意味着当系统将参数从ajax传递到控制器时,事情会变得不对劲。 Somehow the system treats "file" type differently from others. 系统以某种方式区别对待“文件”类型。 Could you help me so the file get passed successfully to the controller and tell me what is wrong with this approach? 您能否帮助我,以便文件成功传递到控制器,并告诉我此方法有什么问题?

As mentioned rightly in the comments use FormData. 如注释中正确提到的,使用FormData。 Below is a code snippet : 下面是一个代码片段:

var fd = new FormData();
fd.append('file', fileObject);
fd.append('PictureId', pictureId);
fd.append('PictureName', pictureName);

    $.ajax({
        url: '/Restaurant/EditPicture',
        async: true,
        type: 'POST',
        data: fd,
        processData: false,
        contentType: false,
        success: function (response) {                  
        }
    });

You cannot upload files with AJAX. 您无法使用AJAX上传文件。 One way to achieve this is to use a hidden iframe which will simulate an AJAX call and perform the actual file upload or use Flash. 实现此目标的一种方法是使用隐藏的iframe,该iframe将模拟AJAX调用并执行实际的文件上传或使用Flash。

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

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