我尝试使用Flex 3,面对上传JPG / PNG图像的问题,跟踪readUTFBytes会返回正确的字节长度,但是tmpFileContent是trucated,它只会通过PHP脚本向服务器上传只有3个字符的数据图像无法使用。我没有非图像格式的问题。这有什么不对?
var tmpFileContent:String = fileRef.data.readUTFBytes(fileRef.data.length);
String是否能够处理字节?
答案 0 :(得分:0)
我不确定您要对图片做什么,但您可能想要阅读此内容:
http://livedocs.adobe.com/flex/3/html/help.html?content=Filesystem_15.html
您可能还需要一个图像编码器,例如JPEGEncoder:http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/mx/graphics/codec/JPEGEncoder.html
答案 1 :(得分:0)
您始终可以使用base64进行编码:
var enc:Base64Encoder = new Base64Encoder();
enc.encodeBytes(fileRef.data);
var base64data:String = enc.drain();
答案 2 :(得分:0)
本教程中使用的方法除了文本文件之外不能安全地工作。任意二进制格式可能包含零。零(一个值为0的字节)通常被认为是许多语言/平台中的字符串终止符。在ActionScript中也是这种情况,因为此代码显示:
var str:String = "abc\x00def";
trace(str);
该字符串将被截断为“abc”,因为0x00被认为是标记字符串的结尾。
我认为你最好的选择是将内容编码为base 64,如maclema建议的那样。从php端,在使用类似的文件写入文件之前将其解码回来:
file_put_contents($myFilePath, base64_decode($fileData["filedata"]));
另外,我不记得file_put_contents是否是二进制安全的(我认为不是)。如果是这种情况,你应该使用fopen('you_path',“wb”),fwrite()和fclose()来编写文件。注意“wb”中的“b”,它代表二进制。如果你没有传递那个标志,你可能会遇到一些字符的问题(例如换行和回车)。
<强>加了:强>
或许,根据davr建议,您可以尝试发送数据ByteArray以查看AMFPHP是否正确处理它。
Php允许在字符串中嵌入Nuls,如下代码所示:
$str = "a\x00b";
var_dump(ord($str{0})); // 97
var_dump(ord($str{1})); // 0
var_dump(ord($str{2})); // 98
因此,如果AMFPHP将bytearray转换为字符串并且在此过程中不会将其变形,那么这实际上可以正常工作。
// method saves files on the server
function uploadFiles($fileData) {
// new file path an name
// to not overwrite the files we add the microtime before the file name
$myFilePath = '../../_uploads/'.
preg_replace("/[^0-9]+/","_",microtime()).'_'.$fileData["filename"];
// writing on the disk
$fp = fopen($myFilePath,"wb");
if($fp) {
fwrite($fp,$fileData["filedata"]);
fclose($fp);
}
// returning response - is not used anywhere
return true;
}
否则,尝试回显var_dump($ fileData ['filedata'])以查看AMFPHP将数据转换为实际类型的内容(可能它使用数组,不确定;给出字符串如何在php中工作(很像缓冲区)但是,我认为单字节字符只能使用字符串。