【问题标题】:post normal form with dropzone使用 dropzone 发布正常形式
【发布时间】:2016-01-31 02:55:08
【问题描述】:

我关注this tutorial 是为了将 DropZone 包含在传统表单元素中:

HTML

<form id="my-awesome-dropzone" class="dropzone">
  <div class="dropzone-previews"></div> <!-- this is were the previews should be shown. -->

  <!-- Now setup your input fields -->
  <input type="email" name="username" />
  <input type="password" name="password" />

  <button type="submit">Submit data and files!</button>
</form>

这里还有 JS

Dropzone.options.myAwesomeDropzone = { // The camelized version of the ID of the form element

  // The configuration we've talked about above
  autoProcessQueue: false,
  uploadMultiple: true,
  parallelUploads: 100,
  maxFiles: 100,

  // The setting up of the dropzone
  init: function() {
    var myDropzone = this;

    // First change the button to actually tell Dropzone to process the queue.
    this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
      // Make sure that the form isn't actually being sent.
      e.preventDefault();
      e.stopPropagation();
      myDropzone.processQueue();
    });

    // Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
    // of the sending event because uploadMultiple is set to true.
    this.on("sendingmultiple", function() {
      // Gets triggered when the form is actually being sent.
      // Hide the success button or the complete form.
    });
    this.on("successmultiple", function(files, response) {
      // Gets triggered when the files have successfully been sent.
      // Redirect user or notify of success.
    });
    this.on("errormultiple", function(files, response) {
      // Gets triggered when there was an error sending the files.
      // Maybe show form again, and notify user of error
    });
  }

}

它工作得很好,除非用户没有提交文件。按照this post,我得做一些修改:

替换简单

myDropzone.processQueue();

通过

var form = $(this).closest('#dropzone-form');
                    if (form.valid() == true) { 
                        if (myDropzone.getQueuedFiles().length > 0) {                        
                            myDropzone.processQueue();  
                        } else {                       
                            myDropzone.uploadFiles([]); //send empty 
                        }                                    
                    }        

现在,正如它在 stackoverflow 帖子“DropZonejs:提交没有文件的表单”评论中所写的那样,我得到了

Uncaught TypeError: Cannot read property 'status' of undefined

所以我检查了dropzone issue 687,它通过替换dropzone.js 的一些内容来解决这个问题。这一行

ata.append(this._getParamName(i), files[i], files[i].name);

到那些行

if ( typeof files[i] != "undefined" ) {
  formData.append(this._getParamName(i), files[i], files[i].name);
} else {
  formData.append(this._getParamName(i), "");
} 

现在它可以工作了(使用模型中的正确数据调用控制器)但是发出的调用是 AJAX 调用,我想在我的应用程序的控制器中进行重定向,所以它不起作用。我可以制作一个带有一个 URL 作为返回的 Json,但我必须将重定向保留在后端。

控制器示例:

[HttpPost]
        public ActionResult Create(CustomViewModel model)
        {
            // Here I get Request.IsAjaxRequest() = true when form is submitted
            if (ModelState.IsValid)
            {
                var container = DoSomething();
                if (container.HasErrors)
                {
                    SetError(container.ErrorMessage);
                    return RedirectToAction("Index");
                }
            }
            else
            {
                SetAlert("ErrorMessage");
            }
            return RedirectToAction("Index");
        }

我该如何解决这个问题? 提前感谢您的帮助

【问题讨论】:

    标签: jquery ajax asp.net-mvc asp.net-mvc-4 dropzone.js


    【解决方案1】:

    我遇到了同样的问题,没有太多时间来解决它。只需要让它尽快工作......

    这是我的 2 美分;

    你可以从你的控制器返回这样的东西;

    return Json(new { ErrorMessage = "", RedirectURL = Url.Action("Get", null, new { id = result.Value.id }, Request.Url.Scheme ) });
    

    并从您发布的 JS 中填写此方法:

    this.on("successmultiple", function (files, response) {
                    // Gets triggered when the files have successfully been sent.
                    // Redirect the user or notify of success.
                    var errorMessage = response.ErrorMessage;
                    if (errorMessage.length > 0) {
                        alert(errorMessage);
                    }
                    else {
                        window.location.replace(response.RedirectURL);
                    }
    };
    

    【讨论】:

    • 即使这不是我想要的解决方案,这对我来说也是可以接受的......谢谢
    【解决方案2】:

    Dropzone 使用 AJAX 上传文件,这意味着页面本身永远不会更改,您不能使用服务器端重定向来更改页面。您唯一的选择是:

    1. 返回带有 URL 的 JSON 对象并在客户端重定向。

    1. 执行正常的表单回发而不是 AJAX 上传。为此,您必须停止使用 Dropzone。

    【讨论】:

    • @Raidi :我不能像我说的那样使用 AJAX,所以这两种响应对我来说都是不可能的。在我展示的第一个链接中,没有使用 Ajax,所以有可能
    • 您的意思是在教程中? AJAX 用于 dropzone 内部(在 processQueue 调用的 uploadFiles(或类似)函数中)。
    猜你喜欢
    • 2018-02-19
    • 1970-01-01
    • 2016-03-14
    • 2017-12-20
    • 2018-10-27
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多