【问题标题】:AIR, URLRequest and uploading video file to NodeJS serverAIR、URLRequest 和上传视频文件到 NodeJS 服务器
【发布时间】:2015-01-28 20:04:56
【问题描述】:

我在本地主机上设置了一个 NodeJS 服务器(用于测试),我用它来对上传的视频文件运行 FFMPEG。这是我要上传到的实际节点应用程序。
https://github.com/madebyhiro/codem-transcode
如果我在 OSX 控制台中使用 curl 作业,实际的转换过程可以正常工作

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 desktop 应用程序来简单地上传视频文件。

很多问题

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

  2. 我的 requestHeaders 中是否缺少某些内容?

  3. 我应该使用 contentType = "multipart/form-data" 还是其他一些 contentType?
  4. contentType 是否应该像我已经完成的那样作为标头的一部分或定义为实际 UrlRequest 对象的属性?
  5. 我应该使用 UrlLoader.load 而不是 File.upload 吗?
  6. file.url 格式是否正确(假设我的 str 值正确)?
  7. 下面我的 uploadFile 代码方法中是否还有其他错误或遗漏?

我将奖励一大笔赏金,但前提是所有上述问题已准确回答,优先考虑带有参考或代码示例的答案。这是另一个相关问题,其中包含一些有用的信息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);
        }

【问题讨论】:

  • 您是否在客户端或服务器端看到任何错误?您的节点服务器是否确实收到了请求?您是否尝试过记录节点端发生的事情?
  • 我已经很久没有做这些事情了,所以我无法帮助编写代码,但这始终是您设置的服务器的问题。使用简单的 hello world 测试代码测试您的服务器设置。

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


【解决方案1】:
  1. 不,您可以将文件上传到任何服务器,无论是本地还是在线
  2. 您没有忘记任何标题。事实上,file.upload 会将您的内容类型标头更正为 multipart/form-data 并设置正确的边界
  3. 见 2。
  4. 见 2。
  5. 不,file.upload 可以正常工作
  6. 格式正确
  7. 好吧,您应该将 JSON 数据作为 URLVariables 发送,以使额外数据起作用。否则 file.upload 将忽略该数据并用文件本身覆盖它。

这是我测试过的代码,可以在这里使用:

Actionscript 代码:(还添加了一个 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);

【讨论】:

    猜你喜欢
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    相关资源
    最近更新 更多