[英]Copy image file from server to disk in PHP
I have a js script that saves an image as .jpg in a specific folder on the server: 我有一个js脚本,将图像另存为.jpg在服务器上的特定文件夹中:
$data = substr($_POST['imageData'], strpos($_POST['imageData'], ",") + 1);
$decodedData = base64_decode($data);
$fp = fopen("imgdownload/cc.jpg", 'wb');
fwrite($fp, $decodedData);
fclose($fp);
The next step would be for the user to save it on his/her disk, the best would be to open a dialog box "save as" and choose the name and location, but just forcing download to a set location would be a dream. 下一步是让用户将其保存在他/她的磁盘上,最好的方法是打开一个“另存为”对话框,然后选择名称和位置,但是仅仅强制下载到设置的位置将是一个梦想。
I've tried: 我试过了:
$file = 'imgdownload/cc.jpg';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
And other things without success. 等事情没有成功。 What am I doing wrong and how to do it right?
我做错了什么,怎么做对了?
EDIT - this is my JS: 编辑-这是我的JS:
savePicture: function() {
$(this.el).find('canvas').attr('id', 'myCanvas');
var data = document.getElementById("myCanvas").toDataURL();
$.post("api/process.php", {
imageData: data
}, function(data) {
//window.location = data;
});
},
You cannot trigger a download in response to a background AJAX request. 您无法响应后台AJAX请求而触发下载。 You'll have to direct the main browser to the URL where the download occurs.
您必须将主浏览器定向到进行下载的URL。 Eg, in your AJAX callback:
例如,在您的AJAX回调中:
window.location = '/download.php';
That means you'll need to store the file server-side in the AJAX upload request to then have it available for download in the separate following request to download.php
somewhere. 这意味着您需要将文件存储在服务器端的AJAX上传请求中,然后才能在单独的以下请求download.php中下载该
download.php
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.