Youtube 通过 URLRequest 上传?

发布于 2024-11-15 08:43:22 字数 663 浏览 2 评论 0原文

我正在尝试通过 YouTube api 上传视频。我可以很好地验证所有内容并很好地制定请求,但我遇到问题的是带有二进制视频数据的请求正文。

对文件数据进行编码并将其添加到 urlRequest 正文的正确方法是什么?

我最好的猜测是:

public function getFileStreamBytes(fileName:String):String{

var byteArray:ByteArray = new ByteArray();
var returnString:String = "";

var file:File = new File(fileName);
var fileStream:FileStream = new FileStream();
fileStream.open(file,FileMode.READ);
fileStream.position = 0;
fileStream.readBytes(byteArray);

byteArray.position = 0;
for(var i:Number = 0; byteArray.bytesAvailable > 0; i++){
    returnString += byteArray.readUTF();

}
return returnString;
}

这会返回 400 Bad Request 响应

I'm attempting to upload a video via the YouTube api. I can authenticate everything fine and formulate the request just fine but the body of the request with the binary video data I'm having an issue with.

What's the proper way to encode the file data and add it to the body of the urlRequest?

My best guess was:

public function getFileStreamBytes(fileName:String):String{

var byteArray:ByteArray = new ByteArray();
var returnString:String = "";

var file:File = new File(fileName);
var fileStream:FileStream = new FileStream();
fileStream.open(file,FileMode.READ);
fileStream.position = 0;
fileStream.readBytes(byteArray);

byteArray.position = 0;
for(var i:Number = 0; byteArray.bytesAvailable > 0; i++){
    returnString += byteArray.readUTF();

}
return returnString;
}

This returns a 400 Bad Request response

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-11-22 08:43:22

我刚刚遇到了一个非常相似的问题,终于得到了答案(归功于 stackoverflow 用户 deadrunk)。事实证明,如果您通过 URLRequest 发送二进制数据,则必须手动将其格式化为 POST 数据。看一下这段代码:

//*** FORMAT POST DATA ***//
var myByteArray:ByteArray = new ByteArray; //Data to be uploaded
var myData:ByteArray = new ByteArray;
var myBoundary:String = "";
var stringData:String;
var i:uint;

for (i = 0; i < 0x20; ++i ) myBoundary += String.fromCharCode(uint(97+Math.random()*25));
myData.writeShort(0x2d2d); //--
myData.writeUTFBytes(myBoundary);
myData.writeShort(0x0d0a); //\r\n
stringData = 'Content-Disposition: form-data; name="fieldName"; filename="filename.txt"';
for (i = 0; i < stringData.length; i++) myData.writeByte(stringData.charCodeAt(i));
myData.writeShort(0x0d0a); //\r\n
stringData = 'Content-Type: application/octet-stream'; //Change me!
myData.writeShort(0x0d0a); //\r\n
myData.writeShort(0x0d0a); //\r\n
for (i = 0; i < stringData.length; i++) myData.writeByte(stringData.charCodeAt(i));
myData.writeBytes(myByteArray, 0, myByteArray.length );
myData.writeShort(0x0d0a); //\r\n
myData.writeShort(0x2d2d); //--
myData.writeUTFBytes(myBoundary);
myData.writeShort(0x2d2d); //--

//*** SEND REQUEST ***//
var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php");
uploadRequest.method = URLRequestMethod.POST;
uploadRequest.contentType = 'multipart/form-data; boundary=' + myBoundary;
uploadRequest.data = myData
uploadRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );

var uploader:URLLoader = new URLLoader;
uploader.dataFormat = URLLoaderDataFormat.BINARY;
uploader.load(uploadRequest);

基本上,您向内容类型添加一个“;boundary=[boundary string]”参数,然后按如下方式格式化您的请求:

--[boundary string]
Content-Disposition: form-data; name="[name of the form field]"; filename="[filename you want the server to see]"
Content-Type: [ideally your data's actual content type]

[your binary data]
--[boundary string]--

我希望有帮助!

I just encountered a very similar problem, and finally got an answer (credit to stackoverflow user deadrunk). It turns out if you're sending binary data via a URLRequest, you have to manually format it as POST data. Have a look at this code:

//*** FORMAT POST DATA ***//
var myByteArray:ByteArray = new ByteArray; //Data to be uploaded
var myData:ByteArray = new ByteArray;
var myBoundary:String = "";
var stringData:String;
var i:uint;

for (i = 0; i < 0x20; ++i ) myBoundary += String.fromCharCode(uint(97+Math.random()*25));
myData.writeShort(0x2d2d); //--
myData.writeUTFBytes(myBoundary);
myData.writeShort(0x0d0a); //\r\n
stringData = 'Content-Disposition: form-data; name="fieldName"; filename="filename.txt"';
for (i = 0; i < stringData.length; i++) myData.writeByte(stringData.charCodeAt(i));
myData.writeShort(0x0d0a); //\r\n
stringData = 'Content-Type: application/octet-stream'; //Change me!
myData.writeShort(0x0d0a); //\r\n
myData.writeShort(0x0d0a); //\r\n
for (i = 0; i < stringData.length; i++) myData.writeByte(stringData.charCodeAt(i));
myData.writeBytes(myByteArray, 0, myByteArray.length );
myData.writeShort(0x0d0a); //\r\n
myData.writeShort(0x2d2d); //--
myData.writeUTFBytes(myBoundary);
myData.writeShort(0x2d2d); //--

//*** SEND REQUEST ***//
var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php");
uploadRequest.method = URLRequestMethod.POST;
uploadRequest.contentType = 'multipart/form-data; boundary=' + myBoundary;
uploadRequest.data = myData
uploadRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );

var uploader:URLLoader = new URLLoader;
uploader.dataFormat = URLLoaderDataFormat.BINARY;
uploader.load(uploadRequest);

Basically you add a ";boundary=[boundary string]" parameter to the content type, then format your request as such:

--[boundary string]
Content-Disposition: form-data; name="[name of the form field]"; filename="[filename you want the server to see]"
Content-Type: [ideally your data's actual content type]

[your binary data]
--[boundary string]--

I hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文