Safari xhr 拖放文件上传似乎发生了两次
它可能与 Webfaction 配置有关(他们有 nginx 代理,我的应用程序是在 apache2+mod_wsgi 下运行的 webpy),因为它在我的 devcherrypy 服务器中工作。
以下是我用于上传的 javascript 代码中的一些内容:
/* Bind drop events */
$(this).bind({
"dragover": function(e){
var dt = e.originalEvent.dataTransfer;
if(!dt) return;
if($.browser.webkit) dt.dropEffect = 'copy';
$(this).addClass("active");
return false;
},
"dragleave": function(e){
$(this).removeClass("active")
},
"dragenter": function(e){return false;},
"drop": function(e){
var dt = e.originalEvent.dataTransfer;
if(!dt&&!dt.files) return;
$(this).removeClass("active")
var files = dt.files;
for (var i = 0; i < files.length; i++) {
upload(files[i]);
}
return false;
}
})
/* Upload function code cut down to the basic */
function upload(file) {
var xhr = new XMLHttpRequest();
var xhr_upload = xhr.upload;
xhr_upload.addEventListener("progress", function(e){
if( e.lengthComputable ) {
var p = Math.round(e.loaded * 100 / e.total );
if(e.loaded == e.total){
console.log( e );
}
}
}, false);
xhr_upload.addEventListener( "load", function( e ){}, false);
xhr_upload.addEventListener( "error", function( error ) { alert("error: " + error); }, false);
xhr.open( 'POST', url, true);
xhr.onreadystatechange = function ( e ) { };
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("Content-Type", file.type);
xhr.setRequestHeader("X-File-Name", encodeURIComponent(file.fileName));
xhr.setRequestHeader("X-File-Size", file.fileSize);
xhr.send(file);
}
如果我在进度事件中用百分比值填充跨度,那么在 Safari 中,它会从 0% 到 100%,然后从 50% 到 100%,然后上传完成。 Chrome 和 Firefox 都可以。
e.loaded == e.total
每次上传都会达到两次,因为我在控制台日志中看到了这一点:
控制台日志http://img824.imageshack.us/img824/4363/screenshot20110827at101.png
在第一个记录的事件中,totalSize 等于文件的大小,但在第二个事件中,它是文件大小的两倍。
It can be related to Webfaction configuration (they have nginx proxy, and my app is webpy running under apache2+mod_wsgi) because it works in my dev cherrypy server.
Here are some bits from javascript code I use for upload:
/* Bind drop events */
$(this).bind({
"dragover": function(e){
var dt = e.originalEvent.dataTransfer;
if(!dt) return;
if($.browser.webkit) dt.dropEffect = 'copy';
$(this).addClass("active");
return false;
},
"dragleave": function(e){
$(this).removeClass("active")
},
"dragenter": function(e){return false;},
"drop": function(e){
var dt = e.originalEvent.dataTransfer;
if(!dt&&!dt.files) return;
$(this).removeClass("active")
var files = dt.files;
for (var i = 0; i < files.length; i++) {
upload(files[i]);
}
return false;
}
})
/* Upload function code cut down to the basic */
function upload(file) {
var xhr = new XMLHttpRequest();
var xhr_upload = xhr.upload;
xhr_upload.addEventListener("progress", function(e){
if( e.lengthComputable ) {
var p = Math.round(e.loaded * 100 / e.total );
if(e.loaded == e.total){
console.log( e );
}
}
}, false);
xhr_upload.addEventListener( "load", function( e ){}, false);
xhr_upload.addEventListener( "error", function( error ) { alert("error: " + error); }, false);
xhr.open( 'POST', url, true);
xhr.onreadystatechange = function ( e ) { };
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("Content-Type", file.type);
xhr.setRequestHeader("X-File-Name", encodeURIComponent(file.fileName));
xhr.setRequestHeader("X-File-Size", file.fileSize);
xhr.send(file);
}
If I fill span with percentage value in progress event, then in Safari it goes from 0% to 100%, then from 50% to 100%, and after that upload is done. Chrome and Firefox are OK.
e.loaded == e.total
is reached twice per upload, since I see this in my console log:
console log http://img824.imageshack.us/img824/4363/screenshot20110827at101.png
In the first logged event totalSize is equal to the size of file, but in the second it is twice as much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尝试大量使用控制台来弄清此类问题的真相。在每次显示有意义的内容的每个主要代码处放置一条控制台语句:
然后再次插入您的
upload()
。I would try heavy use of the console to get to the bottom of something like this. Put a console statement at every major piece of code displaying something meaningful each time:
and then again inslide your
upload()
.它可能与具有相同名称的变量和函数(上传)有关吗?在 Javascript 中,您可以使用函数的名称作为对其的引用。尝试重命名您的
xhr.upload
变量,看看它是否修复了任何问题。Could it have something to do with having a variable and a function with the same name (upload)? In Javascript, you can use the name of a function as a reference to it. Try renaming your
xhr.upload
variable and see if it fixes anything.