如何将 base64 转换为 zip 并移动到服务器

How to convert base64 to zip and move to server

我正在使用 JSZip 压缩一些用户上传文件并将此 zip 文件存储在服务器上。 zip_file 包含我要存储在服务器中的 zip 文件。 zip_file 是 base64 格式,因此,如果我将它作为 LongText 格式存储在 PHPMyAdmin 中,它无法存储一些 zip。是否可以将 zip_file 转换为压缩并移动到目录?如果是的话怎么办?或者如何在 PHPMyAdmin 中存储 base64 值。

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

可以设置returntypeblob,使用XMLHttpRequest()到postBlobphp

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

在 php 使用 php://input,参见 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);

?>