使用JPGEncoder将多个图像从Flash保存到服务器?

时间:2011-07-25 23:52:21

标签: php flash actionscript-3 encoding file-upload

我正在尝试使用JPGEncoder对来自Flash的两个(或更多)图像进行编码和上传。我可以毫无问题地上传单个图像,但从不需要上传两个或更多编码图像。这是我用来编码和上传单个图像的代码。

var bmd:BitmapData = new BitmapData(img.width,img.height,true,0);
bmd.draw(img);

var ba:ByteArray = new JPGEncoder(90).encode(bmd);

var vars:Object = new Object();
vars.path = 'uploads/';

var urlRequest : URLRequest = new URLRequest();
urlRequest.url = 'http://example.com/image.php';
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = UploadPostHelper.getPostData('image.jpg',ba,vars);
urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache'));

var urlLoader : URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, loadComplete);
urlLoader.load(urlRequest);

function loadComplete(e:Event):void {
    trace(e.target.data);
}

此外,我正在使用here is the UploadPostHelper helper class。有没有人这样做过?你能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:2)

如果您将所有JPEG编码和上传内容放入函数中,则可以从loadComplete(e:Event)调用该函数。函数的示例名称可以是urlEncodeUpload()从代码中调用函数一次,当它发出loadComplete信号时,它可以再次调用自身(有点像迭代器,但使用事件驱动的样式)。 / p>

您需要先检查是否有任何图像需要编码和上传。如果您将所有图像都放在自己的数组中,则可以pop()下一个位图,直到它为空,然后将该图像传递给urlEncodeUpload()函数。

function urlEncodeUpload(img:*):void {
        var bmd:BitmapData = new BitmapData(img.width,img.height,true,0);
        bmd.draw(img);

        var ba:ByteArray = new JPGEncoder(90).encode(bmd);

        var vars:Object = new Object();
        vars.path = 'uploads/';

        var urlRequest : URLRequest = new URLRequest();
        urlRequest.url = 'http://example.com/image.php';
        urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
        urlRequest.method = URLRequestMethod.POST;
        urlRequest.data = UploadPostHelper.getPostData('image.jpg',ba,vars);
        urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache'));

        var urlLoader : URLLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
        urlLoader.addEventListener(Event.COMPLETE, loadComplete);
        urlLoader.load(urlRequest);
    }

function loadComplete(e:Event):void {
    if (imgArray.length > 0) urlEncodeUpload(imgArray.pop());
}

希望有所帮助..