Tom
Tom

Reputation: 856

Why is my JavaScript code not successfully uploading my file?

Ok, so I have this JavaScript code. It uploads a file by using a FileReader object. But whenever I try to upload a MID file that contains a + character in it's bytes, it changes that into a space! I know the error is not in the server side script, because I checked out the data on the client side right after the FileReader read my MID file and it was the same as the uploaded end result. I tried reading the file as text as well, but that also fails to work. Please ignore the code comments, I put them in to try different versions of some code without having to erase previous ones. I am fairly sure the error is somewhere in the upload function.

NOTE: For some reason, StackOverflow can not display my code properly. Go to this url to get the full HTML code: http://uploadfilez.tommy3244.com/cgi-bin/index.cgi

I tried a couple of things for a few hours, but some characters (+, but there may be some others) keep getting turned into a space! I can't figure out what the problem is! If I should upload the original and the end result MID files, please tell me. Thanks!

Upvotes: 0

Views: 470

Answers (1)

Jonah
Jonah

Reputation: 16212

A quick look at the source suggests that you are using the javascript escape function when you don't want to be.

Take a look at lines 80-90 in your source code, and then take a look at the docs on escape and the problem should be clear:

http://www.w3schools.com/jsref/jsref_escape.asp

EDIT: 1/2/13

I looked into this some more, and I think my answer above is not the problem. I was able to verify using a breakpoint that the argument being passed to your upload function is correct (ie, I tried with a filename containing "+" chars).

I wasn't able to test with breakpoints inside the upload function, since I don't have your u/p. However, I still believe you'll find the error in the upload function. Specifically, I would stop trying to construct the query string manually, as you do here:

    var data = "";
    //data.append("filename", document.getElementById("filename0").value);
    data += "filename="+escape(document.getElementById("filename0").value);
    //data.append("username", document.getElementById("username").value);
    data += "&username="+escape(document.getElementById("username").value);
    //data.append("password", document.getElementById("password").value);
    data += "&password="+escape(document.getElementById("password").value);
    //data.append("public", document.getElementById("public").value);
    data += "&public="+escape(document.getElementById("public").value);
    //data.append("filebytes", escape(e.target.result));
    data += "&filebytes="+escape(e.target.result);
    //stdlog("Got result! Res: '''"+e.target.result+"'''");

Instead, using JSON to construct the data, like so:

var data = {};
data.filename = escape(document.getElementById("filename0").value);//fixed coding error
// and so on for the other properties

Try that and let me know if it solves your problem.

Upvotes: 1

Related Questions