我在localhost(用于测试)上设置了一个NodeJS服务器,用于在上传的视频文件上运行FFMPEG。这是我上传到的实际节点应用程序。
https://github.com/madebyhiro/codem-transcode
如果我使用
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桌面应用程序来上传视频文件。
很多问题
主要问题是您无法上传文件 同一台机器到本地服务器?
我是否遗漏了来自requestHeaders的内容?
我正在奖励大笔奖金,但前提是所有上述问题已经准确回答,并优先考虑带引用或代码示例的答案。以下是另一个相关问题,其中包含一些有用的信息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);
}
答案 0 :(得分:2)
这是我测试过的代码,在这里工作:
动作脚本代码 :(还添加了一个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);