使用 Dropzone.js 的非 ajax 帖子 [英] Non-ajax post using Dropzone.js

查看:24
本文介绍了使用 Dropzone.js 的非 ajax 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以让 Dropzone.js (http://dropzonejs.com) 与标准浏览器 POST 一起使用的 AJAX.

I'm wondering if there's any way to make Dropzone.js (http://dropzonejs.com) work with a standard browser POST instead of AJAX.

在提交之前,某种方法可以在 DOM 中注入输入 type=file 吗?

Some way to inject the inputs type=file in the DOM right before submit maybe?

推荐答案

没有.出于安全原因,您不能手动设置 的值.当您使用 Javascript 拖放功能时,您完全超越了文件输入.从用户的计算机获取文件后,将文件提交到服务器的唯一方法是通过 AJAX.

No. You cannot manually set the value of a <input type='file'> for security reasons. When you use Javascript drag and drop features you're surpassing the file input altogether. Once a file is fetched from the user's computer the only way to submit the file to the server is via AJAX.

解决方法:您可以改为序列化文件或以其他方式对其进行字符串化并将其作为字符串附加到表单中,然后在服务器端对其进行反序列化.

Workarounds: You could instead serialize the file or otherwise stringify it and append it to the form as a string, and then unserialize it on the server side.

var base64Image;
var reader  = new FileReader();
reader.addEventListener("load", function () {
    base64Image = reader.result;
    // append the base64 encoded image to a form and submit
}, false);
reader.readAsDataURL(file);

也许您使用 dropzone.js 是因为文件输入丑陋且难以设计样式?如果是这种情况,此 Dropzone.js 替代方案 可能适合您.它允许您创建可以与表单一起提交的自定义样式的输入.它也支持拖放,但通过拖放,您无法以您想要的方式提交表单.免责声明:我是上述图书馆的作者.

Perhaps you're using dropzone.js because file inputs are ugly and hard to style? If that is the case, this Dropzone.js alternative may work for you. It allows you to create custom styled inputs that can be submitted with a form. It supports drag and drop too, but with drag and drop you cannot submit the form the way you want. Disclaimer: I am author of aforementioned library.

这篇关于使用 Dropzone.js 的非 ajax 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆