Javascript 如何获取 Dropzone.js 返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26421731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the Dropzone.js return value?
提问by kramer65
I just implemented Dropzone.jsto make file uploads on my website easier. The file uploads fine, and after it finished uploading I give the file an id and return this id to the browser.
我刚刚实施了Dropzone.js以使我网站上的文件上传更容易。文件上传正常,上传完成后我给文件一个 id 并将这个 id 返回给浏览器。
This works fine, except for that I don't know how to catch the id that gets returned from the server. In this SO answerI found some code that should supposedly do that, but it doesn't work for me. The code I have now is pasted below.
这工作正常,除了我不知道如何捕获从服务器返回的 id。在这个 SO 答案中,我发现了一些应该可以做到的代码,但它对我不起作用。我现在拥有的代码粘贴在下面。
Does anybody know how I can get the value that is returned by the server? All tips are welcome!
有人知道我如何获得服务器返回的值吗?欢迎所有提示!
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="/static/js/external/dropzone.min.js"></script>
<link href="/static/css/external/dropzone.css" rel="stylesheet">
<script type="text/javascript">
$(function() {
Dropzone.options.uiDZResume = {
success: function(file, response){
console.log('WE NEVER REACH THIS POINT.');
alert(response);
}
};
});
</script>
</head>
<body>
<form action="/doc"
class="dropzone"
id="my-awesome-dropzone"></form>
</body>
</html>
回答by tomaoq
Looking at the source code of dropzone.js, it seems that there is a lot of events you can listen to.
看dropzone.js的源码,好像可以收听的事件很多。
events: [
"drop"
"dragstart"
"dragend"
"dragenter"
"dragover"
"dragleave"
"addedfile"
"removedfile"
"thumbnail"
"error"
"errormultiple"
"processing"
"processingmultiple"
"uploadprogress"
"totaluploadprogress"
"sending"
"sendingmultiple"
"success"
"successmultiple"
"canceled"
"canceledmultiple"
"complete"
"completemultiple"
"reset"
"maxfilesexceeded"
"maxfilesreached"
]
Here the "success" event seems to be appropriate.
这里的“成功”事件似乎是合适的。
A good starting point would be to bind an event listener to your dropzone and see what data you get on such event.
一个好的起点是将事件侦听器绑定到您的放置区并查看您在此类事件上获得的数据。
$('#my-awesome-dropzone').on('success', function() {
var args = Array.prototype.slice.call(arguments);
// Look at the output in you browser console, if there is something interesting
console.log(args);
});
回答by Rohit Dodiya
$("#dropzoneForm").dropzone({
maxFiles: 2000,
url: "../Uploader/HttpUploadHandler.ashx?param=" + result.prjID,
success: function(file, response){
//alert("Test1");
}
});
回答by code-sushi
Does this help?
这有帮助吗?
Dropzone.options.myDropzone = {
init: function() {
thisDropzone = this;
this.on("success", function(file, responseText) {
var responseText = file.id // or however you would point to your assigned file ID here;
console.log(responseText); // console should show the ID you pointed to
// do stuff with file.id ...
});
}
};
For example, I have mine set up to attach the server path to the image name and pass this as a value into a form field on submit. As long as you define responseText to point to the file ID you should get a return on this.
例如,我已经设置为将服务器路径附加到图像名称,并在提交时将其作为值传递到表单字段中。只要您将 responseText 定义为指向文件 ID,您就应该得到一个回报。
This link might be helpful as well: https://github.com/enyo/dropzone/issues/244
此链接也可能有帮助:https: //github.com/enyo/dropzone/issues/244
回答by Bozu
Try this:
尝试这个:
Dropzone.options.myAwesomeDropzone = {
success: function(file, response){
//alert(response);
console.log(response);
}
};
回答by Jhonny Jr.
It works for me
这个对我有用
$(function() {
var myDropzone = new Dropzone(".dropzone");
myDropzone.on("success", function() {
alert('Uploaded!');
});
});
回答by andy
I am using jQuery and this is what worked for me:
我正在使用 jQuery,这对我有用:
var dz = $(".my-awesome-dropzone").dropzone()[0];
dz.dropzone.on("success", function (file, response) { ... }));
Note that the dropzone()method adds an dropzoneattribute to the DOM object. You have to call on()on thatobject - not the jQuery on().
请注意,该dropzone()方法dropzone向 DOM 对象添加了一个属性。你要调用on()的那个对象-而不是jQuery的on()。
回答by Ovidiu
I wanted to add this as a comment, but I can't, since I have a low reputation.
我想将此添加为评论,但我不能,因为我的声誉很低。
For those of you who still have trouble retrieving the response from the server, if you're using chunking, Dropzone is hard-coding a blank response in this situation:
对于那些仍然无法从服务器检索响应的人,如果您使用分块,Dropzone 在这种情况下硬编码一个空白响应:
if (allFinished) {
_this14.options.chunksUploaded(file, function () {
_this14._finished(files, '', null);
});
}
So retrieving the response doesn't seem to be supported for chunked uploads.
因此,分块上传似乎不支持检索响应。

