LPology / Simple-Ajax-Uploader

Javascript file upload plugin with progress bar support. Works in all major browsers, including IE7+, Chrome, Firefox, Safari, and Opera. No dependencies - use it with or without jQuery.
995 stars 267 forks source link

Failed to execute 'setRequestHeader' on 'XMLHttpRequest' with russian filename #118

Closed is-blackhole closed 8 years ago

is-blackhole commented 9 years ago

All works fine until I have tried file with russian chars (utf-8 name + chromium), I got : Failed to execute 'setRequestHeader' on 'XMLHttpRequest'.

Looks like setting header cause this: headers['X-File-Name'] = fileObj.name;

Think simple encodeURIComponent will fix : headers['X-File-Name'] = encodeURIComponent(fileObj.name);

LPology commented 8 years ago

Try setting the encodeCustomHeaders option to true.

is-blackhole commented 8 years ago

Just tried it, no luck, still got Uncaught SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'русский.yml' is not a valid HTTP header field value.

Looking for code in _uploadXhr:

        for ( var i in headers ) {
            if ( headers.hasOwnProperty( i ) ) {
                if ( opts.encodeCustomHeaders && opts.customHeaders.hasOwnProperty( i ) ) {
                    xhr.setRequestHeader( i, encodeURIComponent( headers[ i ] ) + '' );
                } else {
                    xhr.setRequestHeader( i, headers[ i ] + '' );
                }
            }
        }

only customHeaders will be encoded, but thats not a custom header, but one set at _initUpload method :

    headers['X-Requested-With'] = 'XMLHttpRequest';
    headers['X-File-Name'] = fileObj.name;
LPology commented 8 years ago

Try using this modified version of the plugin:

http://pastebin.com/sm2UJ4wA

I modified line 1522 to look like this:

headers['X-File-Name'] = !this._opts.encodeCustomHeaders ? fileObj.name : encodeURIComponent( fileObj.name );

If that works I'll go ahead and push a change.

is-blackhole commented 8 years ago

yes, its work, thanks!

LPology commented 8 years ago

Glad I could help. Just pushed this change.