How to upload a BitmapData Object straight to my server?

前端 未结 6 485
北海茫月
北海茫月 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: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.

提交回复
热议问题