How to upload a BitmapData object directly to my server?

I want to download from my Flash application (AS3) into the imageshacks XML API. I want to know how I can do this.

"In Flash, we need POST data using the UrlRequest and UrlLoader classes, however we are faced with a limitation of the Flash API. The UrlRequest data property can be either an UrlVariablesByteArray object or not. Value pairs by value along with a JPG byte array. This is a big problem, so as most downloadable applications require a file name and other headers to accompany unprocessed files "

I was hoping someone could help me overcome this!

Thank you all

Update

I tried using this tutorial here: http://www.mikestead.co.uk/2009/01/04/upload-multiple-files-with-a-single-request-in-flash/

The problem is not in unsaved images, but in receiving images from your local computer and then uploading them to a server that already had a name!

+5
source share
7 answers

It helped me a lot: http://kitchen.braitsch.io/upload-bitmapdata-snapshot-to-server-in-as3/

You need to change the URLRequestWrapper to insert field names and file names if necessary. Here is what I did:

bytes = 'Content-Disposition: form-data; name="' + $fieldName + '"; filename="';

It does most formatting of headers so that the server can understand it as a file upload.

, BitmapData, JPEG PNG.

,

+4

, , URLRequest:

var params:URLVariables = new URLVariables();
params.id = ride.id;
params.filename = ride.map_image;
params.path = 'maps/';
var req:URLRequest = new URLRequest( ModelLocator.SERVER_URL + "/php/uploadpic.php");
req.method = URLRequestMethod.GET;
req.data = params;
fileRef.upload(req);

php : $ _REQUEST ['path'] $_REQUEST ['filename'] ..

+2

API- imageshack, BitmapData .

URLRequest bitmapData byteArray, , , :

var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");//this says the data your sending is binary, not text
var uploadRequest:URLRequest = new URLRequest('yourImageShackUploadURL.php');
uploadRequest.requestHeaders.push (header);
uploadRequest.method = URLRequestMethod.POST;
uploadRequest.data = yourBitmapData.getPixels(yourBitmapData.rect);

sendToURL( uploadRequest);

:

import flash.net.* // for sendToURL and other net stuff
import flash.geom.Rectangle;
import flash.display.BitmapData;

ByteArray bitmapData, BitmapData getPixels(). , , Rectangle, , ByteArray BitmapData, , BitmapData rect. , URLRequestHeader, requestHeaders URLRequest. post, .

. API-, , .

!

0

API- imageshack, adobe JPGEncoder - , JPG, .

private function savePicToServer(bmpData:BitmapData):void
{
    var jpgEncoder:JPGEncoder = new JPGEncoder(85);
    var jpgStream:ByteArray = jpgEncoder.encode(bmpData);

    var loader:URLLoader = new URLLoader();
        configureListeners(loader);

    var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
    var request:URLRequest = new URLRequest(ModelLocator.BASE_URL + ModelLocator.UPLOAD_URL + "?user=" + ModelLocator.getInstance().username);
    request.requestHeaders.push(header);
    request.method = URLRequestMethod.POST;
    request.data = jpgStream;
    loader.load(request);
}

, . URLVariables, , URLVariables , byteArray.

0

: , ( ) , URLVariables.

.

var request:URLRequest = new URLRequest();
request.url = "your_server_page_for_example.php";
request.method = URLRequestMethod.POST;

var variables:URLVariables = new URLVariables();
variables.name = "imgName";
variables.type = ".jpg";
variables.image = myByteArray;

request.data = variables;
0
source

You can use URLVariables, just base64-encode bytearray so that it is not binary data:

import mx.utils.Base64Encoder;
// ...
var urlRequest:URLRequest = new URLRequest("/whatever");
urlRequest.method = URLRequestMethod.POST;
var urlVariables:URLVariables = new URLVariables();
var base64Encoder:Base64Encoder = new Base64Encoder();
base64Encoder.encodeBytes(byteArray);
urlVariables.something_base64 = base64Encoder.toString();
urlRequest.data = urlVariables;
navigateToURL(urlRequest, "_blank");

and then on the base64decode base field on the server side, that is:

PHP:

base64_decode($_POST['something_base64'])

Rails:

require 'base64';
# ...
Base64.decode64(params[:something_base64])
0
source

Just my 2 cents for all those here to find the PHP version of upload.php. If you tried HTTP_RAW_POST_DATA and it didn’t work, try file_get_contents ("php: // input");

he worked for me

$data = file_get_contents("php://input");
file_put_contents("filename",$data);
0
source

All Articles