首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AIR:如何在基于POST的web服务(AS3)中使用FileReference.upload()

AIR:如何在基于POST的web服务(AS3)中使用FileReference.upload()
EN

Stack Overflow用户
提问于 2012-01-18 12:03:08
回答 1查看 4K关注 0票数 0

我做的这个小AIR应用程序似乎不能正常工作。舞台上只有一个按钮,当用户按下它时,会显示一个文件浏览器(FileReference.browse()函数)。当他们选择一个图像文件时,我希望图像被上传到站点imgur.com (感兴趣的人使用http://www.api.imgur.com)。

我收到了来自imgur的响应,所以我知道我的应用程序正在连接,但它返回了一个错误,“没有图像数据发送到上传API”。任何帮助都是非常感谢的!(另外,我有一个API密钥,出于显而易见的原因,我将其从这篇文章中删除:)

包{

代码语言:javascript
复制
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.utils.ByteArray;


public class Document extends MovieClip
{
    //file browser var
    var myFileReference:FileReference = new FileReference();

    //file loader var
    private var loader:URLLoader;

    //string constants
    private const API_KEY:String = "******REMOVED*******";
    private const UPLOAD_URL:String = "http://api.imgur.com/2/upload.xml";

    public function Document()
    {
        // constructor code
        browse_btn.addEventListener(MouseEvent.CLICK, browse);
    }

    function browse(e:MouseEvent)
    {
        var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
        myFileReference.browse([imagesFilter]);
        myFileReference.addEventListener(Event.SELECT, selectHandler);
        myFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);
    }

    function selectHandler(e:Event):void
    {                               
        var vars:String = "?key=" + API_KEY + "&name=name&title=title";
        var params:URLVariables = new URLVariables();
        params.date = new Date();
        var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
        //request.contentType = "application/octet-stream";
         request.contentType = "multipart/form-data";
        request.method = URLRequestMethod.POST;
        request.data = params;

        myFileReference.upload(request);
        try 
        { 
            myFileReference.upload(request); 
        } 
        catch (error:Error) 
        { 
        trace("Unable to upload file."); 
        } 
    }

    function responseHandler(e:DataEvent):void
    {
        trace("responseHandler() initaialized");
        var res:XML = XML(e.data);
        trace(res);
    }
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-18 16:33:17

在他们的网站上有一个关于AS3的例子:

代码语言:javascript
复制
package
{
    import com.adobe.images.PNGEncoder;
    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.SecurityErrorEvent;
    import flash.events.IOErrorEvent
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.utils.ByteArray;

    public class ImgurExample
    {
        private var loader:URLLoader;

        private const API_KEY:String = "<api key>";
        private const UPLOAD_URL:String = "http://api.imgur.com/2/upload.xml";

        public function ImgurExample() {

            loader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
            loader.addEventListener(Event.COMPLETE, onCookieSent);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
            loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);

            // Create a bitmapdata instance of a movieclip on the stage.
            var mc:MovieClip;
            var b:BitmapData = new BitmapData(mc.width, mc.height, true);
            b.draw(mc);
            var png:ByteArray = PNGEncoder.encode(b);

            var vars:String = "?key=" + API_KEY + "&name=name&title=title";
            var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
            request.contentType = "application/octet-stream";
            request.method = URLRequestMethod.POST;
            request.data = png; // set the data object of the request to your image.

            loader.load(request);
        }
        // privates
        private function onSend(e:Event):void {
            var res:XML = new XML(unescape(loader.data));
            trace(res);
        }
        private function onIOError(e:IOErrorEvent):void {
            trace("ioErrorHandler: " + e);
            // Handle error
        }
        private function onSecurityError(e:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + e);
            // handle error
        }

    }
}

http://api.imgur.com/examples

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8905016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档