我正在尝试在Flex 4中上传图片,但还没有取得如此大的成功。有人可以帮忙吗?
我的代码:
private var fileref:FileReference;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
fileref = new FileReference();
}
protected function button1_clickHandler(event:MouseEvent):void
{
fileref.browse();
fileref.addEventListener(Event.SELECT, fileSelect);
}
private function fileSelect(e:Event):void {
try {
var data:ByteArray = e.target as ByteArray;
fileref.save(data, "pic1.jpg");
}
catch(err:Error) {
Alert.show("Error: " + err.message);
}
}
修改:
这是真正简单的待办事项:
private function fileComplete(e:Event):void {
if(fileref.data != null) {
image1.data = fileref.data;
}
}
我做了第二个按钮,它应该保存图像,它工作正常,但我得到了对话框 起来,真的有必要吗?如何防止它并将其明确地放在服务器光盘上? 另一种选择(我使用.NET作为后端)来获取bytearray-image并通过.net webservice发送它并让C#代码保存图像。也许这是一个更好的选择。 Actionscript 3可能会对它的功能有一些限制,或者我是否真的没有通知?
protected function button2_clickHandler(event:MouseEvent):void
{
fileref = new FileReference();
var data:ByteArray = image1.data as ByteArray;
if(data != null) {
fileref.save(data);
}
else {
Alert.show("Picture is null!");
}
}
当我采用Webservice方法并在SqlServer中存储Image(= bytearray)时,这很有效。
答案 0 :(得分:1)
要捕获图像并上传它,您可能需要查看ImageSnapshot
。
它会将图像编码为JPEG或PNG。保存该bytearray将为您提供更大的灵活性,并将存储在服务器中的数据与客户端的实现分离。