问题描述
我正在尝试上传一个字节数组,但我正在努力使内容类型成为图像/png(无论我做什么,它总是作为应用程序/八位字节流).
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.).
我已经通过 Charles Proxy 检查了请求,可以确认它确实始终以 application/octet-stream 的形式运行.
I've checked the request with Charles Proxy, and can confirm that it indeed always goes as application/octet-stream.
我的代码是:
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);
}
如您所见,我将内容类型作为multipart/form-data"传递.我试过将它作为 image/png 传递,这使得请求本身按预期作为 image/png 进行,但是,图像本身仍然作为 application/octet-stream.
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.
这是我现在的请求:
--rojpkvwesrtjvrgjbwtadelavgcmxjot 内容配置:表单数据;名称="键"
WZL0NXBE6dcb9af80668bb96b86fa72f5595422c--rojpkvwesrtjvrgjbwtadelavgcmxjot 内容配置:表单数据;名称="文件名"
WZL0NXBE6dcb9af80668bb96b86fa72f5595422c --rojpkvwesrtjvrgjbwtadelavgcmxjot Content-Disposition: form-data; name="Filename"
加拿大.png--rojpkvwesrtjvrgjbwtadelavgcmxjot 内容配置:表单数据;名称=文件上传";filename="Canada.png" 内容类型:应用程序/八位字节流
Canada.png --rojpkvwesrtjvrgjbwtadelavgcmxjot Content-Disposition: form-data; name="fileupload"; filename="Canada.png" Content-Type: application/octet-stream
‰PNG -- 这里以字节数组形式加载图像信息
如您所见,Canada.png 作为 application/octet-stream 传入.我已经摆弄了请求,并手动使其工作,至于我的服务器期望接收的内容,与上面完全相同,但它说:
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:
加拿大.png--rojpkvwesrtjvrgjbwtadelavgcmxjot 内容配置:表单数据;名称=文件上传";filename="Canada.png" 内容类型:应用程序/八位字节流
应该说:
加拿大.png--rojpkvwesrtjvrgjbwtadelavgcmxjot 内容配置:表单数据;名称=文件上传";filename="Canada.png" 内容类型:图像/png
我的问题是如何改变这一点,以便字节数组本身作为图像/png而不是八位字节流传入
My question is how to change this, so the bytearray itself gets passed in as image/png instead of octet-stream
这张图片正在从客户端上传,并作为文件引用传递,然后我调用 load(),然后获取它的数据"属性,这是字节数组所在的位置.
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.
这里的任何帮助将不胜感激,因为我已经为此苦苦挣扎了几个小时,但似乎无法更改字节数组的 mime 类型.
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.
提前致谢,
推荐答案
请求中的内容类型在 UploadPostHelper 类中设置.据我所知,它被硬编码为应用程序/八位字节流.如果您只想上传 png 图像,则可以将其更改为 image/png.或者您可以使用另一个 url 请求包装器来上传允许您指定文件内容类型的文件.例如 MultipartURLLoader.
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.
这篇关于通过 URLRequest 上传字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!