所有,
我正在尝试使用HTML5 API上传一个xml文件。
我已经有一个脚本,这是伟大的图像工作。
它也与一个xls文件工作,除了事实,我不能使用excel打开文件,一旦它已经上传…如果我比较一下大小,原始文件是251 904字节,而上传的文件是251 911字节。我不知道那是否重要。
这是我所做的:
Javascript:reader = new FileReader();
reader.onloadend = function(evt) {
that.file = evt.target.result;
};
reader.readAsDataURL(file); // I've also try with readAsText but it's worse
使用Sproutcore Framework发送文件:
SC.Request.postUrl(url).json()
.notify(this, 'fileUploadDidComplete', fileName)
.send({ fileName: fileName, file: file });
PHP: $httpContent = fopen('php://input', 'r');
$json = stream_get_contents($httpContent);
fclose($httpContent);
$data = json_decode($json, true);
$file = base64_decode($data['file']);
$fileName = substr(md5(time().rand()), 0, 10).$data['fileName'];
$filePath = GX_PATH."/files/tmp/".$fileName;
$handle = fopen($filePath, "w");
fwrite ($handle, $file);
fclose($handle);
我希望有人能帮我找出问题所在。
提前感谢。
编辑:我找到了另一种可以在更多浏览器上工作的方法。我发现了FormData !
formData = new FormData();
formData.append("file", file);
SC.Request.postUrl(url).notify(this, 'formDataDidPost').send(formData);
这样,PHP代码就更简单了:
$_FILES['file'];
我使用以下方法完成了此操作
$out = fopen($fileUploadPath ,"wb");
$in = fopen("php://input", "rb");
stream_copy_to_stream($in,$out);
这只是我用来将上传的内容写入服务器的3种方法。试试