[whatwg] multipart/form-data when POSTing through an XMLHttpRequest (FileApi file)

Am 17.09.2010, 20:46 Uhr, schrieb Anne van Kesteren <annevk at opera.com>:

> On Fri, 17 Sep 2010 20:41:19 +0200, Dennis Joachimsthaler  
> <dennis at efjot.de> wrote:

(Or, any knowledge how it is done today? I can't find
>> anything about it! Crazy.)

> Of course:  
> http://dev.w3.org/2006/webapi/XMLHttpRequest-2/#the-formdata-interface

Oh? It doesn't seem to allow me to insert files though. And where do I set  
the content-type?

Currently I am trying it like this:


xhr.open("POST", "/Post/Add");
xhr.setRequestHeader("Content-Type", "multipart/form-data;  
boundary=HELLOWORLD");
xhr.send("--HELLOWORLD\r\n");
xhr.send("Content-Disposition: form-data; name=\"tags\"\r\n");
xhr.send("\r\n");
xhr.send("tags");
xhr.send("\r\n");
xhr.send("--HELLOWORLD\r\n");
xhr.send("Content-Disposition: form-data; name=\"title\"\r\n");
xhr.send("\r\n");
xhr.send("tags");
xhr.send("\r\n");
xhr.send("--HELLOWORLD\r\n");
xhr.send("Content-Disposition: form-data; name=\"file\"\r\n;  
filename=\"Hallo.jpg\"\r\n");
xhr.send("Content-Type: " + "image/jpeg\r\n");
xhr.send("\r\n");
xhr.sendAsBinary(file.getAsBinary());
xhr.send("\r\n");
xhr.send("--HELLOWORLD--\r\n");

Downside is that the first send() will end the request already.

The file variable is a file gotten from the FileApi.

Received on Friday, 17 September 2010 11:56:46 UTC