How to upload a BitmapData Object straight to my server?

前端 未结 6 465
北海茫月
北海茫月 2020-12-18 08:14

I wish to upload from my Flash Application (AS3) to imageshacks XML API. I wish to know how I can do this.

\"In Flash, we must POST the data using the UrlRequest and

相关标签:
6条回答
  • 2020-12-18 08:28

    This was of great help to me: http://kitchen.braitsch.io/upload-bitmapdata-snapshot-to-server-in-as3/

    You need to modify the URLRequestWrapper to insert field names and file names where needed. Here's what I've done:

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

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

    By the way, if you have a BitmapData you might need to encode it to JPEG or PNG first.

    Regards, Artem

    0 讨论(0)
  • 2020-12-18 08:33

    You can use URLVariables, just base64-encode bytearray so it's 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");
    

    then on server side base64decode '' post field, ie:

    php:

    base64_decode($_POST['something_base64'])
    

    rails:

    require 'base64';
    # ...
    Base64.decode64(params[:something_base64])
    
    0 讨论(0)
  • 2020-12-18 08:37

    I haven't used the imageshack API, but you may want to try using adobe's JPGEncoder class - here's a quick example that passes a username and the JPG's byte array, it's really quite simple.

    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);
    }
    

    Note that the variables are passed as part of the query string. You can't use URLVariables, as several people have suggested, as the URLVariables would be stored in the request's data property, which we are already using to pass the byteArray.

    0 讨论(0)
  • 2020-12-18 08:42

    You can send your filename data and any other data you want along with the 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);
    

    on the server side in php you access the extra variables as: $_REQUEST['path'] and $_REQUEST['filename'] etc

    0 讨论(0)
  • 2020-12-18 08:53

    i don't really get the problem of the question, anyway: If the question is how to send the name of the file and the extension (image type) from flash toghether with the byteArray of image data you should use a URLVariables object.

    This object could also send the query string as byte instead of text.

    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 讨论(0)
  • 2020-12-18 08:54

    Just my 2 cents to all those who're here to find a php implementation of upload.php. If you have tried HTTP_RAW_POST_DATA and it hasn't worked, try file_get_contents("php://input");

    it has worked for me

    $data = file_get_contents("php://input");
    file_put_contents("filename",$data);
    
    0 讨论(0)
提交回复
热议问题