How to convert base64 to zip and move to server

最后都变了- 提交于 2019-12-25 05:02:03

问题


I am using JSZip for zipping some user upload file and store this zip file on a server. zip_file contains the zip file which I want to store in a server. zip_file is in base64 format so, if I stored it in PHPMyAdmin as LongText format, its could not store some zip. Is it possible to convert zip_file to zipping and move to a directory? If yes how can do it? Or How to store base64 value in PHPMyAdmin.

zip.generateAsync({type:"base64"}).then(function (content) {
   zip_file = "data:application/zip;base64," + content;
   //zip_file convert and move to /uploads folder
});

回答1:


You can set return type to blob, use XMLHttpRequest() to post Blob to php

zip.generateAsync({type:"blob"}).then(function (content) {
   var request = new XMLHttpRequest();
   request.open("POST", "/path/to/server");
   request.send(content);
});

at php use php://input, see Beyond $_POST, $_GET and $_FILE: Working with Blob in JavaScript and PHP

<?php

  // choose a filename
  $filename = "file.zip";

  // the Blob will be in the input stream, so we use php://input
  $input = fopen('php://input', 'rb');
  $file = fopen($filename, 'wb'); 

  // Note: we don't need open and stream to stream, 
  // we could've used file_get_contents and file_put_contents
  stream_copy_to_stream($input, $file);
  fclose($input);
  fclose($file);

?>


来源:https://stackoverflow.com/questions/38934833/how-to-convert-base64-to-zip-and-move-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!