MVC 5 - 使用FormData

时间:2017-04-21 07:14:41

标签: javascript asp.net-mvc forms asp.net-mvc-5 multipartform-data

我使用formData.append("PermissionList", ["Owner", "Resident"]);将一串字符串传递给控制器​​。我收到的值为string[],只有1个值,看起来像这样:Owner,Resident。如何将数组作为FormData传递?

这是JavaScript代码:

var formData = new FormData();
var files = $("#file").get(0).files;
if (files.length > 0) { 
        formData.append("ReportFile", files[0]);
    }
    else {
        alert("No file is chosen");
        return;
    }
    formData.append("PermissionList", ["Owner","Resident"]);
    formData.append("ReportName", $('#report-name-input').val());

    $.ajax({
        cache: false,
        url: 'Upload',
        type: 'POST',
        async: true,
        traditional: true,
        data: formData,
        processData: false,
        contentType: false,
        success: function (data) {
            window.location = data;
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr.status);
            console.log(thrownError);
        }
    });

这是我在控制器中收到它的方式:

    public ActionResult Upload(HttpPostedFileBase ReportFile, string ReportName, string[] PermissionList)
    {
        Debug.WriteLine("hey " + ReportFile.FileName + " " + ReportName + " " + PermissionList.Length + " " + PermissionList[0]);

        return Content(Url.Action("Index", "Reporting"));
    }

输出结果为:

hey MonthlyReport.rdl OtherReport 1 Owner,Resident

0 个答案:

没有答案