如何使用 Filereference 在 Flex 4 中上传图像?
我正在尝试在 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 Web 服务发送它,并让 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!");
}
}
当我采用 Web 服务方法并将图像(= bytearray)存储在 SqlServer 中时,这种方法效果很好。
I'm trying to upload images in Flex 4 and haven't got so much success with it yet. Somebody who can help?
My code:
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);
}
}
Edit:
This was real simple todo:
private function fileComplete(e:Event):void {
if(fileref.data != null) {
image1.data = fileref.data;
}
}
I made a second button which should save the image, and it works fine, but I get the dialog
up, is it really necessary? How can I prevent it and put it explicitly on the servers disc?
An alternative (I use .NET as a backend) to take the bytearray-image and send it through a .net webservice and let the C# code save the image. Maybe that's an better option. Actionscript 3 may have some limitations on what it can do, or am I not really informed?
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!");
}
}
This worked well when I took the Webservice approach and stored the Image (= bytearray) in SqlServer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要捕获图像并上传,您可能需要查看
ImageSnapshot
。它将图像编码为 JPEG 或 PNG。保存该字节数组将为您提供更大的灵活性,并将服务器中存储的数据与客户端的实现分离。
to capture an image and upload it you may want to have a look at
ImageSnapshot
.It will encode the image into either JPEG or PNG. Saving that bytearray will give you more flexibility and decouple the data stored in the server from the client's implementation.