使用带有formData的xhr ajax在第一个请求上传文件后发送附加请求

时间:2015-08-11 11:59:56

标签: javascript asp.net ajax asp.net-mvc file-upload

我花了很多时间尝试在localhost上模拟此错误,但没有成功。这个错误发生在web的实时版本上。从实时版本的网络发送错误电子邮件。

这是错误

The parameters dictionary contains a null entry for parameter 'isJsonResponse' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult UploadFileDocument(Model, Boolean)' in Controller'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

这是javascript:

 $('#uploadFile').click(function () {            

        var input = $('#file')[0];

        if (input.files.length == 0) return;

        $(".titleRed", $('#files')).remove();

        var dObject = new Object();
        dObject.TypeID = $('#types').val();
        dObject.FileName = input.files[0].name;
        dObject.File = input.files[0];
        dObject.ID = '@HttpUtility.JavaScriptStringEncode(Model.ID)';

        var formData = new FormData();
        for (var key in dObject) {
            formData.append(key, dObject[key]);
        }

        formData.append('isJsonResponse', false);
        uploadFiles(formData, function (result) {

            if (typeof result == 'object') {
                console.log(result.msg);
            } else {
                $('#files').append(result);
                $('#fileAlert').remove();
            }



        });
            document.getElementById('file').value = '';
            $('#types').val('1');

        return false;
    });

功能uploadFile:

function uploadFiles(formData, updateCallback,progressCallback,startCallback,errorCallback) {

    var xhr = new XMLHttpRequest();
    xhr.open('POST', _uploadFilesURL);


    //listner on progress (%) for progress bar
    xhr.upload.addEventListener("progress", function (evt) {
        if (evt.lengthComputable) {
            if (typeof progressCallback != "undefined") {

                //count %
                var percentComplete = evt.loaded / evt.total;
                percentComplete = parseInt(percentComplete * 100);
                debugLog(percentComplete);
                progressCallback(percentComplete, this.progressId);
            }
        }
    }, false);

    // when downloading start create unique ID for progress bar
    xhr.upload.addEventListener("loadstart", function (e) {
        if (typeof startCallback != "undefined") {
            // generating uniqe ID (GUID)
            this.progressId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : r & 0x3 | 0x8; return v.toString(16); });
            startCallback(this.progressId);
        }
    });

    //callback on final result of request
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var data = null;

            //if crash JSON.Parse then is possible that result is pure text(partialView)
            try {
                data = JSON.parse(xhr.responseText);                    

            } catch (e) {

            }
            if (data == null) {                  
                //supose it is pure text if json parse crash
                updateCallback(xhr.responseText);
            } else {
                updateCallback(data.result);
            }

        } else if (xhr.readyState == 4 && xhr.status != 200) {
            if (typeof errorCallback != "undefined") {
                errorCallback(xhr);
            }
        }
    }

    xhr.send(formData);

}

这是Asp net MVC方法代码:

     [HttpPost]
public ActionResult UploadFile(Model mode, bool isJsonResponse)
    {

型号:

 public class Model
{      
    public string FileName { get; set; }      
    public int TypeID { get; set; }
    public int? ModelID { get; set; }

    public HttpPostedFileBase File { get; set; }
    public int ID { get; set; }

}

该文件是正常上传的。但在那之后,有时会发送另一个请求但没有fileBody和其他参数如isJsonResponse。帖子只包含TypeID和FileName。

错误来自IE,FF,Chrome

有人有经验吗?或类似的东西

0 个答案:

没有答案