FileReference和HttpService浏览图片修改它然后上传它

fra*_*nck 1 apache-flex upload httpservice filereference gumbo

我正在尝试做一个图像上传器,用户可以:
- 使用button.browse浏览本地文件
- 选择一个并将其保存为FileReference.
- 然后我们做FileReference.load()然后将数据绑定到我们的图像控件.
- 在我们对其进行旋转并更改图像数据之后.
- 并完成我们将其上传到服务器.

要更改图像数据,我将获得显示图像的矩阵并对其进行转换,然后重新使用新矩阵并将其绑定到我的旧图像:

private function TurnImage():void
{ 
    //Turn it
    var m:Matrix = _img.transform.matrix;
    rotateImage(m);
    _img.transform.matrix = m;
}
Run Code Online (Sandbox Code Playgroud)

现在主要的是我真的不知道如何将数据作为文件发送到我的服务器,因为它没有存储在FileReference中,FileReference中的数据是readOnly所以我们无法更改它或创建新的,所以我可以不要使用.upload();.

然后我尝试了HttpService.send,但我无法弄清楚你是如何发送文件而不是mxml的.

小智 5

您可以使用URLLoader将Binary ByteArray发送到服务器,例如:

var urlRequest : URLRequest = new URLRequest();
urlRequest.url = 'path to your server';
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = UploadPostHelper.getPostData( 'image.jpg', byteArray );
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );

// create the image loader & send the image to the server:<br />
var urlLoader : URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load( urlRequest );
Run Code Online (Sandbox Code Playgroud)

首先获取图像的bitmapdata:

// set up a new bitmapdata object that matches the dimensions of the captureContainer;
var bmd : BitmapData = new BitmapData( captureContainer.width, captureContainer.height, true, 0xFFFFFFFF );

// draw the bitmapData from the captureContainer to the bitmapData object:<br />
bmd.draw( captureContainer, new Matrix(), null, null, null, true );
Run Code Online (Sandbox Code Playgroud)

然后得到byteArray:

var byteArray : ByteArray = new JPGEncoder( 90 ).encode( bmd );
Run Code Online (Sandbox Code Playgroud)

并使用上面的URLLoader代码将图像发送到服务器.

它会正常工作,除了你不会像FileReference.upload那样获得文件上传进度.如果您可以使用URLLoader进行上传进度,请在此处发布您的答案.