开发者

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

开发者 https://www.devze.com 2023-04-03 08:42 出处:网络
I all, I\'m trying to upload an xls file using the HTML5 API. I already have a script which is working开发者_如何转开发 great for images.

I all,

I'm trying to upload an xls file using the HTML5 API.

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. I don't know if that matter.

Here is what i've done :

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:

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

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 = new FormData();
formData.append("file", file);
SC.Request.postUrl(url).notify(this, 'formDataDidPost').send(formData);

This way, the PHP code is more simple :

$_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. Try it and see

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号