我有一个表单,用户可以使用此代码添加一个或多个附件
<div id="attachments">
</div>
<a href="#" id="addAttachment">Add attachment</a>
<script>
$("#addAttachment").click(function () {
var strChooseFile = "<table><tr><td><input type='file' name='PostedFiles' size='30' title='Choose file ...'></td>";
strChooseFile += "<td><input type='text' name='FileDescriptions' size='50' title='File descriptions ...'></td>";
strChooseFile += "<td><input type='button' name='btnAttachmentRemove' id='btnRemoveAttachment' value='Remove' ></td></tr></table>";
inputFile = $(strChooseFile);
inputFile.clone(true).appendTo("#attachments");
});
</script>
我跟着this tutorial准备数据,然后再发送到服务器。我在代码下面如何准备数据:
$("#btnAddInvoice").click(function (e) {
if ($("#frmInvoice").validate().form()) {
var m_data = new FormData();
m_data.append("SubsidiaryClientID", $("#SubsidiaryClientID").val());
m_data.append("SubsidiaryClientNo", $("#SubsidiaryClientNo").val());
m_data.append("SubsidiaryClientName", $("#SubsidiaryClientName").val());
m_data.append("PostedFiles", $("input[name=PostedFiles]").val()); // how to parse this to json?
m_data.append("FileDescriptions", $("input[name=FileDescriptions]").val()); // how to parse this to json?
$.ajax({
type: "POST",
url: "@Url.Content("~/Invoice/AddInvoice")",
data: m_data,
contentType: false,
processData: false,
dataType: "json",
cache: false
}).done(function (data) {
alert(data);
if (!data.message) {
$("#invoiceList").html(data);
$("#addInvoiceModal").modal("hide");
}
});
}
});
正如您在上面的代码中看到的那样,我在准备PostedFiles
和FileDescriptions
时遇到了问题。发送到控制器后,值为空。
如何从PostedFiles
和$("input[name=PostedFiles]")
的数组中准备FileDescriptions
?