从ASP.NET MVC中的控制器收到响应后,如何调用Dropzone的processQueue来上传文件?

时间:2019-08-13 16:50:43

标签: javascript c# asp.net-mvc dropzone

我收到来自对控制器的ajax调用的响应后,尝试使用processQueue启动fileupload。在用户选择文件并将其拖放到Dropzone容器后,用户提交调用控制器的表单。该控制器具有生成唯一编号的代码。一旦DropZone启动文件上载(调用文件上载控制器),我就使用此数字在Azure Blob存储中创建一个Blob容器。在创建此Blob之前,我无法上传文件。生成唯一编号后,如何在此ajax调用内调用processQueue?

我已经测试过,看看processQueue是否可以通过按钮单击事件来上传文件。该代码可以正常工作,但是从ajax响应内部进行的调用无济于事。我也尝试在ajax响应中触发按钮单击事件,但是没有运气。

var myDropzone;

    $(document).ready(function () {
        Dropzone.autoDiscover = false;
        var token = $('input[name="__RequestVerificationToken"]').val();
        console.log(token);
       var UrlUpload = '@Url.Content("~/")' +"FileUpload/UploadFiles";
        myDropzone = new Dropzone("#dZUpload", {
            autoQueue: true,
            autoDiscover : false,
            url: UrlUpload,
            headers: { "__RequestVerificationToken": token },
            maxFiles: 20,
            addRemoveLinks: true,
            maxFilesize: 1,
            acceptedFiles: ".png,.jpg,.gif,.bmp,.jpeg,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.psd,.txt",
            autoProcessQueue: false,
            parallelUploads: 1,
            init: function () {
                var startUpload = document.getElementById('btnSubmit');
                var myDropzone = this;
                startUpload.addEventListener("click", function () {
                   myDropzone.processQueue();
                });
                @*this.on("processing", function (file) {
                    myDropzone.options.url = '@Url.Content("~/")' +"FileUpload/UploadFiles";
                });*@
                this.on("success", function () {
                    myDropzone.options.ProcessQueue = true;
                });
                this.on("queuecomplete", function () {
                    setTimeout(function () {
                        myDropzone.options.autoProcessQueue = false;
                        myDropzone.removeAllFiles(true);
                    }, 10000);
                });
            },
            success: function (file, response) {
                file.previewElement.classList.add("dz-success");
            },
            error: function (file, response) {
                file.previewElement.classList.add("dz-error");
                $(file.previewElement).find('.dz-error-message').text(response);
                console.log(response);
            }
        });

// AJAX call

 function SaveAndSendApprove() {
         var url = "@Url.Action("SendPCNforApproval")";
        var id = PCNID;
        $.get(url, { PCNnumber: id }, function (res) {
            if (res.message == "Success") {

                $('#Create').attr("disabled", true);
                $('#SaveAndSend').show(); $('#SaveAndSend').attr("disabled", true);
                $('#Send').hide();

                //call click event of btnsubmit for upload files to start
                $('#btnSubmit').trigger('click');
                //myDropzone.processQueue();
                //var myfilezone = Dropzone.forElement(".dropzone");
                //myfilezone.processQueue();


                 smoke.okalert("PCN successfully saved and sent for approval  PCNID:" + PCNID, function (e) {
                        if (e) {
                            window.location.href = '@Url.Action("Index", "PCNDashboard")';
                        } else {
                            window.location.href = '@Url.Action("Index", "PCNDashboard")';
                        }
                    }, {  ok: "Ok" });
            } else {`enter code here`
                smoke.signal('Error occured,please try again');
            }

        });
        }

0 个答案:

没有答案