我想使用 IFormFile 在我的数据库中上传一些文档。在客户端,我使用的是 Dropzone.js,但它不能正常工作。每当我点击提交按钮时,它都会在参数中发送一个空值。我有两个疑问。1.如何将文档(图像/PDF)发送到操作方法。2.如何重新设计(改变风格)Dropzone。(如果有人解释 dropzone 的流程,那将非常有用)
这是我的 HTML 代码
<form asp-action="AddDocument" asp-controller="Transactions" method="post" enctype="multipart/form-data" class="dropzone dz-clickable form-horizontal form-bordered" id="dropzoneForm">
<div class="form-group form-actions">
<div class="col-md-9 col-md-offset-4">
<button type="submit" class="btn btn-sm btn-primary"><i class="fa fa-floppy-o"></i> Upload</button>
</div>
</div>
</form>
JS代码:
$(function () {
Dropzone.options.dropzoneForm = {
paramName: "DocumentPhotos", // The name that will be used to transfer the file
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 100,
accept: function (file, done) {
done();
}
};
});
这是我的服务器端语言代码:
[HttpPost]
public async Task<IActionResult> AddDocument(IEnumerable<IFormFile> DocumentPhotos)
{
if (DocumentPhotos!= null)
{
TransactionViewModel viewModel = new TransactionViewModel();
var documentModel = viewModel.TransactionModel.TransactionDocuemnt;
foreach (var item in DocumentPhotos)
{
using (MemoryStream stream = new MemoryStream())
{
await item.CopyToAsync(stream);
documentModel.DocumentPhoto = stream.ToArray();
}
documentModel.DocumentName = item.FileName;
documentModel.SrNo = 1;
documentModel.SocietyRid = UserSessionState.ApplicationUser.SocietyRid;
}
}
return PartialView("TransactionDocumentForm", transactionViewModel);
}
使用的脚本和 CSS(所有文件均从 Dropzone.js 下载):
<link rel="stylesheet" href="~/css/basic.css" />
<link rel="stylesheet" href="~/css/dropzone.css" />
<script type="text/javascript" src="~/js/dropzone.js"></script>
<script type="text/javascript" src="~/js/dropzone-amd-module.js"></script>