为多个文件上传附加formdata

时间:2016-03-16 21:24:08

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

我的formdata在IE9浏览器中返回undefined。我正在尝试解决它

这是我想要做的。如果formdata未定义,我试图手动将数据附加到formdata。但我不知道如何附加多个文件。 非常感谢帮助。

脚本

$('#btnUploadFiles').click(function (event) {
        fnBlockUI();
        event.preventDefault();
        var data = new FormData($('#formAssignment')[0]);

        alert(data);

        if (data == undefined)
        {
            //var data = new FormData();
            //var fileLen = $('#txtFile').get(0).files.length;

            //alert(fileLen);

            //for (var i = 0; i < fileLen; i++) {
            //    data.append('txtFile[]', $('#txtFile').file[i]);
            //}

            var ProjectName = $('#ddlProjectName').val();
            var CEQRNumber = $('#ddlCEQRNumber').val();
            data.append("CEQRNumber", CEQRNumber);
            data.append("ProjectName", ProjectName);
        };
        $.ajax({
            url: '@Url.Action("ViewProjectAssignment", "AssignmentOfProject", new { Area = "PrivateCEQRApplication" })',
            type: 'POST',
            dataType: 'json',
            cache: false,
            headers: headers,
            data: data,
            beforeSend: function (xhr, settings) { xhr.setRequestHeader('__RequestVerificationToken', token); },
            contentType: false,
            processData: false,
            success: function (result) {
                $.unblockUI();
            },
            error: function (xhr, textStatus, errorThrown) {
                $.unblockUI();
            }
        });
    });

我的表格

@using (Html.BeginForm("ViewProjectAssignment", "AssignmentOfProject", FormMethod.Post, new { enctype = "multipart/form-data", @id = "formAssignment" }))
{
    @Html.AntiForgeryToken()
    <table class="headertable">
        <thead>
            <tr>
                <th colspan="2" align="left">
                    Search Project for Uploading Files
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td style="padding-left:50px; width:120px;">
                    <b>Project Name</b>
                </td>
                <td>
                    @Html.DropDownListFor(mod => mod.ProjectName, Model.ProjectNameList, new { @class = "textBoxDisplay", @id = "ddlProjectName", style = "width:300px;" })
                    <div id="divProjectName" style="display:inline;"></div>
                </td>
            </tr>
            <tr>
                <td style="padding-left:50px; width:120px;">
                    <b>CEQR Number</b>
                </td>
                <td>
                    @Html.DropDownListFor(mod => mod.CEQRNumber, Model.CEQRNumberList, new { @class = "textBoxDisplay", @id = "ddlCEQRNumber" })
                    <div id="divCEQRNumber" style="display:inline;"></div>
                </td>
            </tr>
            <tr>
                <td style="padding-left:50px; width:120px;">
                    <b>Upload File</b>
                </td>
                <td>
                    @Html.TextBoxFor(mod => mod.File, new { class = "textBoxFileDisplay", style = "width:600px;", type = "file", multiple = "true", @id = "txtFile" })
                    <div id="divFile" style="display:inline;"></div>
                </td>
            </tr>
            <tr>
                <td style="padding-left:50px; width:120px;"></td>
                <td>
                    @*<input class="button" value="Upload File" type="submit" />*@
                    <button name="button" class="button" value="UploadFile" id="btnUploadFiles">
                        Upload File
                    </button>
                </td>
            </tr>
        </tbody>
    </table>
}

0 个答案:

没有答案