如何将base64转换为zip并移动到服务器 [英] How to convert base64 to zip and move to server

查看:1365
本文介绍了如何将base64转换为zip并移动到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您可以将return type设置为blob,使用XMLHttpRequest()Blob发布到php

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

在php中使用php://input,请参见

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

?>

这篇关于如何将base64转换为zip并移动到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆