I'm trying to upload a bytearray, but am struggling to make the content-type go as image/png (it always goes as application/octet-stream no matter what I do.).
I've checked the request with Charles Proxy, and can confirm that it indeed always goes as application/octet-stream.
My code is:
protected function onObjectLoaded(event:Event):void
{
var byteArray:ByteArray = new ByteArray();
var fileName:String = fileReference.name;
var uploadPath:String = serviceURL;
var parameters:Object = new Object();
parameters.key = serviceKey;
fileReference.data.readBytes(byteArray,0,fileReference.data.length);
var urlRequest:URLRequest = new URLRequest();
urlRequest.url = uploadPath;
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = UploadPostHelper.getPostData(fileName, byteArray, "fileupload", parameters);
urlRequest.requestHeaders.push( new URLRequestHeader( 'Content-type', 'image/png' ) );
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
/*urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);*/
urlLoader.addEventListener(Event.COMPLETE, processResults);
urlLoader.load(urlRequest);
}
As you can see, I;m passing the content type as "multipart/form-data". I've tried passing it as image/png, which made the request itself go as image/png as expected, however, the image itself still goes as application/octet-stream.
Here's what my request looks like right now:
--rojpkvwesrtjvrgjbwtadelavgcmxjot Content-Disposition: form-data; name="key"
WZL0NXBE6dcb9af80668bb96b86fa72f5595422c --rojpkvwesrtjvrgjbwtadelavgcmxjot Content-Disposition: form-data; name="Filename"
Canada.png --rojpkvwesrtjvrgjbwtadelavgcmxjot Content-Disposition: form-data; name="fileupload"; filename="Canada.png" Content-Type: application/octet-stream
‰PNG -- Loads of image information here as a bytearray
As you can see Canada.png is being passed in as application/octet-stream. I've fiddled with the request, and have manually made it work, as to what my server expected to receive, which is exactly the same as above, but where it says:
Canada.png --rojpkvwesrtjvrgjbwtadelavgcmxjot Content-Disposition: form-data; name="fileupload"; filename="Canada.png" Content-Type: application/octet-stream
It should say:
Canada.png --rojpkvwesrtjvrgjbwtadelavgcmxjot Content-Disposition: form-data; name="fileupload"; filename="Canada.png" Content-Type: image/png
My question is how to change this, so the bytearray itself gets passed in as image/png instead of octet-stream
This image is being upload from the client side, and being passed as a FileReference, which I then invoke load(), and then get it's "data" attribute, which is where the bytearray is.
Any help here would be much appreciated, as I've been struggling with this for a few hours, but can't seem to be able to change the mime type of the bytearray.
Thanks in advance,
The content-type in your request is set inside UploadPostHelper class. As far as I can tell it is hard-coded as application/octet-stream. You can just change it to image/png if you only going to upload png images. Or you can use another url request wrapper for uploading files that allows you to specify content-type of a file. For example MultipartURLLoader.