Pavel Tzonkov
Pavel Tzonkov

Reputation: 272

Posting unicode characters with Ajax

I'm trying to integrate HTML5 drag and drop file upload functionality to my project KCFinder. The code is written by someone from the forum. It works fine. The only problem if you are trying to upload files into a directory with name contains special unicode characters (cyrillic etc...). The directory path, files should be uploaded to, is transferred via $_GET['dir']. If I use upload button instead of drag'n'drop the upload works and I see

Content-Disposition: form-data; name="dir"

files/ÑеÑÑ

in POST data (using Firebug). The real directory path is files/тест. If I drag and drop files from my computer to the same directory, I got an error

String contains an invalid character" code: "5
xhr.sendAsBinary(postbody); 

The drag and drop works without errors if the directory name I'm trying to upload into, has only non-unicode characters. I tried to encode 'dir' parameter with encodeURIComponent(), but in this case the error is gone, but the responsed PHP gets it encoded ("files/%D1%82%D0%B5%D1%81%D1%82")

postbody += 'Content-Type: ' + evt.target.thisFileType + '\r\n\r\n' +
    evt.target.result + '\r\n--' + boundary +
    '\r\nContent-Disposition: form-data; name="dir"\r\n\r\n' +
    encodeURIComponent(evt.target.thisTargetDir) + '\r\n--' + boundary + '\r\n--' +
    boundary + '--\r\n';

The project has Git. I cannot post more hyperlinks. You can found it at the SourceForge project.

Upvotes: 1

Views: 960

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477444

JavaScript strings are encoded with UTF-16, as you can see in the URL-encoded string (0xD182 is a UTF-16 code unit, etc.). You should probably encode the string as an explicit UTF-8 string in your AJAX request.

Upvotes: 3

Related Questions