开发者

Safari xhr drag'n'drop file upload seems to occur twice

开发者 https://www.devze.com 2023-03-31 00:42 出处:网络
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.

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 );
         开发者_StackOverflow   }
        }
    }, 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.


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:

for (var i = 0; i < files.length; i++) 
{
console.log(files[i]+", "+i);    
upload(files[i])
}; 

and then again inslide your 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消