简体   繁体   English

进度回调始终在 jquery-file-upload 插件中显示 100% 上传

[英]progress callback always shows 100% upload in jquery-file-upload plugin

I want to implement a progress bar for the blueimp jquery-file-upload plugin but the progress callback is only fired once immediately after the upload begins and data.loaded == data.total.我想为 blueimp jquery-file-upload 插件实现一个进度条,但progress回调只在上传开始和 data.loaded == data.total 后立即触发一次。

<input data-url="/ads/32/photos" id="image" name="image" type="file" />

$(".upload-btn input").fileupload({
    dataType: "json",
    progress: function (e, data) {
        alert(data.loaded + "/" + data.total);
    },
    done: function (e, data) {
        alert("done");
    }
});

Seconds later (when the upload completes) the done callback is fired and I can confirm via server logs that the upload was successful.几秒钟后(上传完成时)完成回调被触发,我可以通过服务器日志确认上传成功。 I get the same behavior when I try to use the progressall callback as well.当我尝试使用progressall回调时,我也得到了相同的行为。

Not sure if it's related, but I'm not currently including the jquery.fileupload-fp.js library since adding that prevents any upload activity from happening what so ever.不确定它是否相关,但我目前不包括 jquery.fileupload-fp.js 库,因为添加它可以防止任何上传活动发生。

Any idea what I'm doing wrong?知道我做错了什么吗?

Is the progress function alert ing the string "100%"?进度函数是否alert字符串“100%”? I'm unfamiliar with the plugin, but the expression in alert for the progress function casts everything to a String type.我不熟悉该插件,但是progress函数的alert表达式将所有内容都转换为String类型。 So I would expect that it shows the string "x/y".所以我希望它显示字符串“x/y”。

In any case you should change the progress function to在任何情况下,您都应该将progress功能更改为

alert( data.loaded / data.total ); // no quotes, no +'s

If those attributes ( total and loaded ) are correct, then it should give you the behavior you want.如果这些属性( totalloaded )是正确的,那么它应该给你你想要的行为。

Edit: To clarify what's happening, if this answered your question: the + operator behaves in different ways according to the data type of its operands.编辑:为了澄清发生了什么,如果这回答了您的问题: +运算符根据其操作数的数据类型以不同的方式运行。 If you + two numbers together, it works as an arithmetical addition operator.如果你+两个数,它可以作为一个算术加法运算符。 If even one of the operators is a string, the + will first change the other operands to strings first, and then concatenate them.如果其中一个运算符是字符串, +将首先将其他操作数更改为字符串,然后它们连接起来。 The end result will be a string, not a number.最终结果将是一个字符串,而不是一个数字。

var two = "2"; // two is a String data type
alert(two + 2); // Returns "22" as a String

var two = 2;
alert(two + 2); // Returns 4

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Addition https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Addition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM