我只是遇到了一个非常类似的问题,终于得到了一个答案(信贷计算器用户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 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]--
我希望帮助!
当您尝试此操作时会发生什么? – 2011-06-13 19:23:05
发送您发送到服务器的标题信息。我们不能修复格式错误的头文件,而不会看到头文件 – 2011-06-13 21:44:21
而在您发布数据时,您只发布了获取文件数据的代码。 – 2011-06-13 21:45:28