I'm writing a chrome extension content script which will embed itself on some pages, and when there are certain file type links (.doc, .torrent, etc) it will download that file, and then do a file POST to a python web server which will save that file. The python server is working, and handles a normal multipart/form-data POST request, and successfully saves the file when I use the html interface I wrote for it.
I have javascript downloading the file properly:
var req = new XMLHttpRequest(); req.open('GET', 'http://foo.com/bar.torrent', false); req.overrideMimeType('text/plain; charset=x-user-defined'); req.send(null); if (req.status != 200) return ''; var response = req.responseText; And then when I try to create a POST request and upload it
// Define a boundary, I stole this from IE but you can use any string AFAIK var boundary = "---------------------------7da24f2e50046"; var xhr = new XMLHttpRequest(); var body = '--' + boundary + '\r\n' // Parameter name is "file" and local filename is "temp.txt" + 'Content-Disposition: form-data; name="upfile";' + 'filename="temp.torrent"\r\n' // Add the file's mime-type + 'Content-type: application/bittorrent\r\n\r\n' + response + '\r\n'; //+ boundary + '--'; xhr.open("POST", "http://python.server/", true); xhr.setRequestHeader( "Content-type", "multipart/form-data; boundary="+boundary ); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) alert("File uploaded!"); } xhr.send(body); It thinks that it uploaded successfully, but when I try to open the file it says the data is corrupted. I think this is some kind of encoding issue, but I'm not 100% sure.
Any thoughts would be very helpful.