gpt4 book ai didi

json - AIR、URLRequest 并将视频文件上传到 NodeJS 服务器

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:27 25 4
gpt4 key购买 nike

我在本地主机上设置了一个 NodeJS 服务器(用于测试),用于在上传的视频文件上运行 FFMPEG。这是我要上传到的实际 Node 应用程序。
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

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

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

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

很多问题

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

  2. 我的 requestHeaders 中是否遗漏了某些内容?

  3. 我应该使用 contentType = "multipart/form-data"还是其他 contentType?
  4. contentType 是否应该像我所做的那样成为 header 的一部分或定义为实际 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);
}

最佳答案

  1. 不,您可以将文件上传到任何服务器,无论是本地服务器还是在线服务器
  2. 您没有忘记任何标题。事实上,file.upload 会将您的内容类型 header 更正为 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 Server(使用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);

关于json - AIR、URLRequest 并将视频文件上传到 NodeJS 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com