AIR,URLRequest并将videofile upload到NodeJS服务器

我有一个NodeJS服务器设置在本地主机(用于testing),我正在使用上传的video文件运行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 

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

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

在客户端,我使用的AIR桌面应用程序简单地上传video文件。

许多问题

  1. 主要问题只是您不能将同一台计算机上的file upload到本地服务器?

  2. 我从我的requestHeaders错过了什么吗?

  3. 我应该使用contentType =“multipart / form-data”还是其他一些contentType?
  4. 应该contentType作为标题的一部分,我已经做了或定义为实际的UrlRequest对象的属性?
  5. 我应该使用UrlLoader.load而不是File.upload?
  6. file.url格式正确(假设我的str值是正确的)?
  7. 在我的uploadFile代码方法下面的任何其他错误或遗漏?

如果所有上述问题都得到了准确的答复,我还是要奖励一大笔奖金,但是最好还是参考答案或代码样本。 这里是另一个相关的问题,一些有用的信息POSTfile upload使用URLRequest

这里是相关的上传代码。 str是我正在上传的实际video文件的本地path。 如前所述,JSON 参数对象已被故意留空,所以需要适当的格式才能正常工作。

 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. 不,你可以上传文件到任何服务器,无论是本地还是在线
  2. 你没有忘记任何标题。 事实上,file.upload会将你的内容types头修正为multipart / form-data并设置正确的边界
  3. 见2。
  4. 见2。
  5. 不,file.upload可以正常工作
  6. 格式是正确的
  7. 那么,你应该发送JSON数据作为URLVariables额外的数据工作。 否则,file.upload将忽略该数据并用文件本身覆盖它。

这是我testing的代码,并在这里工作:

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和Muller 0.1.7来处理file upload):

 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);