简体   繁体   English

使用HTML5 File API在PHP服务器上上传.xls文件

[英]Upload .xls file on PHP server using the HTML5 File API

I all, 我都

I'm trying to upload an xls file using the HTML5 API. 我正在尝试使用HTML5 API上传xls文件。

I already have a script which is working great for images. 我已经有一个脚本,非常适合图像。

It's also working with an xls file except the fact that I can't open the file using excel once it has been uploaded... If I compare the size, the orignal file is 251 904 octets while the upload one is 251 911 octets. 它也可以与xls文件一起工作,除了上载后无法使用excel打开文件...如果比较大小,原始文件为251904个八位字节,而上载为251911个八位字节。 I don't know if that matter. 我不知道这是否重要。

Here is what i've done : 这是我所做的:

Javascript: 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

the file is send using Sproutcore Framework: 使用Sproutcore Framework发送文件:

SC.Request.postUrl(url).json()
  .notify(this, 'fileUploadDidComplete', fileName)
  .send({ fileName: fileName, file: file });

PHP: 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);

I hope somebody will help me to find what is wrong. 我希望有人能帮助我找出问题所在。

Thanks in advance. 提前致谢。

EDIT: 编辑:

I find another way which work on more browsers. 我找到了在更多浏览器上工作的另一种方式。 I've discover FormData ! 我发现了FormData!

formData = new FormData();
formData.append("file", file);
SC.Request.postUrl(url).notify(this, 'formDataDidPost').send(formData);

This way, the PHP code is more simple : 这样,PHP代码更加简单:

$_FILES['file'];

I have done this using following method 我已经使用以下方法完成了此操作

$out = fopen($fileUploadPath ,"wb");
$in = fopen("php://input", "rb");
stream_copy_to_stream($in,$out);

This is just 3 methods I used to write the uploaded content to the server. 这只是我用来将上传的内容写入服务器的3种方法。 Try it and see 试试看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM