0

我想使用 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>
4

1 回答 1

3

如果您将该选项uploadMultiple设置为 true,则 Dropzone 将附加[]到 paramName。在查看了来自 html5 多文件上传的请求后,我注意到该请求没有将索引添加到文件名 (files[n])。Dropzone.js 这样做是为了解决这个问题。如果您将 paramName 选项添加到 Dropzone JS 配置并让它调用返回文件的方法,您将获得与 html5 多文件上传相同的行为。您还可以参考此链接以获取有关 Dropzone.js 的配置选项的更多详细信息

function myParamName() {
    return "DocumentPhotos";
}

Dropzone.options.dropzoneForm = {
   paramName: myParamName, 
   //other configuration options
}

autoProcessQueue: false 告诉 Dropzone 在您删除文件时不要自动上传文件,默认情况下会这样做。由于 Dropzone 现在不会自动上传文件,因此您需要在单击按钮时手动告诉它这样做。因此,您需要一个用于该按钮单击的事件处理程序,它告诉 Dropzone 进行上传:

 <button type="submit" id="submit" class="btn btn-sm btn-primary"><i class="fa fa-floppy-o"></i> Upload</button>

 var wrapperThis = this;

 $("#submit").click(function (e) {
       e.preventDefault();
       wrapperThis.processQueue();
 });

完整的代码

<form asp-action="AddDocument" asp-controller="Home" 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" id="submit" class="btn btn-sm btn-primary"><i class="fa fa-floppy-o"></i> Upload</button>
    </div>
  </div>
</form>

@section Scripts
{
  <link rel="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/dropzone.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/dropzone.js"> </script>
  <script>
       function myParamName() {
           return "DocumentPhotos";
        }
        Dropzone.options.dropzoneForm = {
            autoProcessQueue: false,
            paramName: myParamName, // The name that will be used to transfer the file
            uploadMultiple: true,
            parallelUploads: 100,
            init: function () {
                console.log("active");
                var wrapperThis = this;

                $("#submit").click(function (e) {
                    e.preventDefault();
                    wrapperThis.processQueue();
                });
            },
            accept: function (file, done) {
                done();
            }
        };

</script>
}
于 2019-11-13T04:30:03.863 回答