如何解决上传表单并接收服务器消息后如何重新发送 dropzone 文件
我有一个表单,其中包含我使用 DropZone 库提交的文章信息和图像。 我对这个库没有任何问题,它工作得很好,但是当提交的表单有错误并且我通过 Ajax 在客户端收到此错误消息时,用户修复了问题并再次发送表单,但不幸的是表单是没有发送,也没有留下任何文件。未选中的 虽然文件在预览中可用并且仅发送到服务器一次。 我应该怎么做才能解决这个问题? 请输入简单的代码。 谢谢
成功多重功能
myDropzone.on("successmultiple",function(file,serverResponse) {
/* None of the uploaded files are available in Drop Zone here anymore,** and I had to delete the files so the user could choose again,** which would not be a good user experience.
** Exactly what code should I write here so that there is no need to
** re-select files from the user's system?
*/
myDropzone.removeFile(file);
if(serverResponse.status)
{
// Success:: In this case,I have no problem
alert("Article saved successfully. Redirecting to the Articles page ...");
window.location.href = serverResponse.redirectedTo;
}
else
{
// Display errors received from the server to the user
alert("Please enter your name and resubmit the form.");
}
});
解决方法
我认为一个可能的解决方案是,如果您将事件传递给您的成功处理程序并阻止它的默认行为。 像这样:
function successHandler(event){
event.preventDefault();
}
这应该防止刷新页面和丢失输入中的文件。 否则我只会将文件保存到一个变量中。
,我自己找到了我的问题的答案,我也会把它放在下面给你。
此代码是为 Laravel 刀片文件编写的:
<script>
$("document").ready(()=>{
var path = "{{ $path }}";
var file = new File([path],"{{ $attach->file_name }}",{type: "{{ $attach->mime_type }}",lastModified: {{ $attach->updated_at}}})
file['status'] = "queued";
file['status'] = "queued";
file['previewElement'] = "div.dz-preview.dz-image-preview";
file['previewTemplate'] = "div.dz-preview.dz-image-preview";
file['_removeLink'] = "a.dz-remove";
file['webkitRelativePath'] = "";
file['width'] = 500;
file['height'] = 500;
file['accepted'] = true;
file['dataURL'] = path;
file['upload'] = {
bytesSent: 0,filename: "{{ $attach->file_name }}",progress: 0,total: {{ $attach->file_size }},uuid: "{{ md5($attach->id) }}",};
myDropzone.emit("addedfile",file,path);
myDropzone.emit("thumbnail",path);
// myDropzone.emit("complete",itemInfo);
// myDropzone.options.maxFiles = myDropzone.options.maxFiles - 1;
myDropzone.files.push(file);
console.log(file);
});
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。