Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
public function sendImage(bmpData:BitmapData, url:String):void
{
//Converting BitmapData into a PNG-encoded ByteArray
var imageBytes: ByteArray = PNGEncoder.encode(bmpData);
imageBytes.position = 0;
var boundary: String = '---------------------------7d76d1b56035e';
var header1: String = '--'+boundary + '\r\n'
+'Content-Disposition: form-data; name="poster"\r\n\r\n'
+'picture.jpg\r\n'
+'--'+boundary + '\r\n'
+'Content-Disposition: form-data; name="poster"; filename="picture.png"\r\n'
+'Content-Type: application/octet-stream\r\n\r\n';
//In a normal POST header, you'd find the image data here
var header2: String = '--'+boundary + '\r\n'
+'Content-Disposition: form-data; name="Upload"\r\n\r\n'
+'Submit Query\r\n'
+'--' + boundary + '--';
//Encoding the two string parts of the header
var headerBytes1: ByteArray = new ByteArray();
headerBytes1.writeMultiByte(header1, "ascii");
var headerBytes2: ByteArray = new ByteArray();
headerBytes2.writeMultiByte(header2, "ascii");
//Creating one final ByteArray
var sendBytes: ByteArray = new ByteArray();
sendBytes.writeBytes(headerBytes1, 0, headerBytes1.length);
sendBytes.writeBytes(imageBytes, 0, imageBytes.length);
sendBytes.writeBytes(headerBytes2, 0, headerBytes2.length);
var request: URLRequest = new URLRequest(url);
request.data = sendBytes;
request.method = URLRequestMethod.POST;
request.contentType = "multipart/form-data; boundary=" + boundary;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, uploadComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
try {
loader.load(request);
} catch (error: Error) {
trace("Unable to load requested document.");
}
}try {
loader.load(request);
} catch (error: Error) {
trace("Unable to load requested document.");
}
}
Опыт создания загрузчика изображений