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

How to convert base64 to zip and move to server

本文关键字:移动 服务器 zip base64 转换      更新时间:2023-09-26

我使用JSZip压缩一些用户上传的文件,并将这个压缩文件存储在服务器上。zip_file包含我想要存储在服务器中的zip文件。zip_file是base64格式,所以,如果我把它存储在PHPMyAdmin作为LongText格式,它不能存储一些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
});

您可以设置返回typeblob,使用XMLHttpRequest()Blob发送到php

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

在php使用php://input,参见超越$_POST, $_GET和$_FILE:在JavaScript和php中使用Blob

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