gpt4 book ai didi

jquery - 带 dropzone 的后正规形式

转载 作者:行者123 更新时间:2023-12-01 00:22:18 25 4
gpt4 key购买 nike

我关注了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), "");
}

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

Controller 示例:

[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");
}

如何解决这个问题?预先感谢您的帮助

最佳答案

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

这是我的 2 美分;

您可以从 Controller 返回类似的内容;

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);
}
};

关于jquery - 带 dropzone 的后正规形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33440098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com