AIR,URLRequest并将视频文件上传到NodeJS服务器

时间:2015-01-28 20:04:57

标签: json node.js actionscript-3 post urlrequest

我在localhost(用于测试)上设置了一个NodeJS服务器,用于在上传的视频文件上运行FFMPEG。这是我上传到的实际节点应用程序。
https://github.com/madebyhiro/codem-transcode
如果我使用

在OSX控制台中运行卷曲作业,则实际的转换过程可以正常工作
sudo curl -d '{"source_file": "MASTER.flv","destination_file":"converted.mp4","encoder_options": "-vcodec libx264 -vb 416k -s 320x180 -y -threads 0"}' http://localhost:8080/jobs

所以我知道节点服务器正在正常运行。

您可以看到HTTP POST请求中需要特定的JSON对象。 (在我的AIR客户端代码示例中,这是 params 对象,我故意留空。)

在客户端,我使用AIR for桌面应用程序来上传视频文件。

很多问题

  1. 主要问题是您无法上传文件     同一台机器到本地服务器?

  2. 我是否遗漏了来自requestHeaders的内容?

  3. 我应该使用contentType =" multipart / form-data"或其他一些contentType?
  4. 在我已经完成或定义为实际UrlRequest对象的属性时,contentType应该是标题的一部分吗?
  5. 我应该使用UrlLoader.load而不是File.upload吗?
  6. file.url 格式正确(假设 str 值正确)?
  7. 我的 uploadFile 代码方法中的任何其他错误或遗漏?
  8. 我正在奖励大笔奖金,但前提是所有上述问题已经准确回答,并优先考虑带引用或代码示例的答案。以下是另一个相关问题,其中包含一些有用的信息POST file upload using URLRequest

    以下是相关的上传代码。 str 是我上传的实际视频文件的nativePath。如前所述,JSON params 对象有意留空,因此需要正确的格式才能正常工作。

    function uploadFile(str:String):void {
                var params:Object={}
                var jsonOb:String = JSON.stringify(params);
                var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
                var request:URLRequest=new URLRequest("http://localhost:8080");
                request.requestHeaders.push(hdr);
                request.method=URLRequestMethod.POST;
                request.useCache=false;
                request.cacheResponse=false;
                //pass urlVariables instead of JSON Object??
                request.data=jsonOb;
    
                var file:File=new File();
                configureListeners(file);
                file.url='file:///'+str;
    
                try {
                    file.upload(request);
                } catch (e:Error) {
                    trace('error', e);
                }
    
            }
    
            private function configureListeners(dispatcher:IEventDispatcher):void {
                dispatcher.addEventListener(ProgressEvent.PROGRESS, uploadProgressHandler, false, 0, false);
                dispatcher.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpResponseHandler, false, 0, false);
                dispatcher.addEventListener(Event.COMPLETE, uploadCompleteHandler, false, 0, true);
                dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
                dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
                dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
            }
    

1 个答案:

答案 0 :(得分:2)

  1. 不,您可以将文件上传到任何服务器,无论是本地还是在线
  2. 你没有忘记任何标题。事实上,file.upload会将你的内容类型标题更正为multipart / form-data并设置正确的边界
  3. 见2.
  4. 见2.
  5. 不,file.upload可以正常工作
  6. 格式正确
  7. 那么,您应该将JSON数据作为URLVariables 发送,以使额外数据生效。否则file.upload将忽略该数据并用文件本身覆盖它。
  8. 这是我测试过的代码,在这里工作:

    动作脚本代码 :(还添加了一个UPLOAD_COMPLETE_DATA侦听器,以便在上传时获取nodejs响应)

    uploadFile("/Users/wouter/test.jpg");
    
    private function uploadFile(str:String):void {
        //additional data needs to be a URLVariables instance
        var data:URLVariables = new URLVariables();
        data.origFile = str;
        //no use, upload will reset headers
        //var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
        var request:URLRequest=new URLRequest("http://localhost:8080");
        //request.requestHeaders.push(hdr);
        request.method=URLRequestMethod.POST;
        request.useCache=false;
        request.cacheResponse=false;
        //pass urlVariables instead of JSON Object??
        request.data=data;
    
        var file:File=new File();
        configureListeners(file);
        file.url='file:///'+str;
    
        try {
            file.upload(request);
        } catch (e:Error) {
            trace('error', e);
        }
    
    }
    
    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(ProgressEvent.PROGRESS, uploadProgressHandler, false, 0, false);
        dispatcher.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpResponseHandler, false, 0, false);
        dispatcher.addEventListener(Event.COMPLETE, uploadCompleteHandler, false, 0, true);
        dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
        dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
        //add a UPLOAD_COMPLETE_DATA event to process server response
        dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete, false, 0, true);
    }
    

    NodeJS服务器(使用express 4.0和multer 0.1.7处理文件上传):

    var express = require('express'),
        multer  = require('multer');
    var app = express();
    
    //auto save file to uploads folder
    app.use(multer({ dest: './uploads/'}))
    
    app.post('/', function (req, res) {
        console.log(req.body); //contains the variables
        console.log(req.files); //contains the file references
        res.send('Thank you for uploading!');
    });
    
    app.listen(8080);