1

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

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

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

现在问题是我真的不知道如何将数据作为文件发送到我的服务器,因为它没有存储在 FileReference 中,并且 FileReference 中的数据是只读的,所以我们不能更改它或创建一个新的,所以我可以'不要使用 .upload();。

然后我尝试了 HttpService.send 但我不知道你是如何发送文件而不是 mxml 的。

4

1 回答 1

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

首先获取图像的位图数据:

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

然后得到字节数组:

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

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

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

于 2009-10-04T16:36:34.933 回答