【发布时间】:2020-10-25 02:55:33
【问题描述】:
我在获取文件阅读器内容分配时遇到了困难。无论如何要等待文件阅读器完成加载并在将文件内容推送到数组之前分配文件内容?
我有一个输入文件类型按钮列表如下:
@for (int i = 0; i < @Model.LetterList.Count(); i++)
{
<tr>
<td>
<input type="file" id="LetterAttachment" name="LetterAttachment" accept="application/pdf">
</td>
</tr>
}
当我点击提交时,我想在循环中将文件内容值分配到我的表单列表中,下面是我的 javascript 代码:
var attach=""; // empty variable for assign file content
function ApproverAction(action) {
var formList = [];
$("input[name='LetterAttachment']").each(function () {
if (this.files && this.files[0]) {
// I perform file reader here to assign the file content into attach....
var FR = new FileReader();
FR.onload = function (e) {
attach = e.target.result;
}
FR.readAsDataURL(this.files[0]);
var form = {
ID: newGuid(),
FileContents: attach, <<< ---- However it showing empty
DocumentName: this.files[0].name,
DocumentSize: this.files[0].size,
DocumentContentType: 'application/pdf',
SourceType: 'OnlineAssessment',
CreatedDate: '@DateTime.Now'
}
formList.push(form);
}
});
console.log(formList);
}
但是我不能完全正确地得到输出结果:
非常感谢任何帮助和提示!谢谢!
【问题讨论】:
标签: javascript c# jquery asp.net-mvc asp.net-mvc-5