php - 是否可以不在服务器上然后在客户端上下载解码文件,而直接在客户端电脑上?
问题描述
所以我正在使用 Gmail API。为了获取附件,我获取了必须使用 base64 解码的数据。但这与问题完全无关。现在我允许用户像这样下载图片/文件:
$attachment = $service->users_messages_attachments->get($userId, $_GET["messageId"], $_GET["attachment_id"]);
$data = $attachment->getData();
$data = strtr($data, array('-' => '+', '_' => '/'));
$myfile = fopen("picture.jpg", "w+");;
fwrite($myfile, base64_decode($data));
fclose($myfile);
echo "<a href= 'picture.jpg' download= 'picture.jpg'>Download</a>";
它工作得很好,但我认为我会使用太多的服务器空间(我将每个图片/文件保存在服务器上,然后允许用户下载它)。我可以将它直接下载到客户端电脑上,而不在服务器上保存图片/文件吗?
解决方案
您正在做的是将数据写入文件,然后输出包含指向该文件的链接的 HTML 页面。您可以简单地输出一个指向 PHP 页面的链接,而不是这样做,该页面将输出该文件的数据。
因此,您的链接将如下所示:
<a href='download.php?messageId=42&attachment_id=69' download='picture.jpg'>Download</a>
这download.php
会做这样的事情:
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="picture.jpg"');
$attachment = $service->users_messages_attachments->get($userId, $_GET["messageId"], $_GET["attachment_id"]);
$data = $attachment->getData();
echo strtr($data, array('-' => '+', '_' => '/'));
推荐阅读
- responsive-design - Chrome 的 Galaxy Fold 响应式视图基于什么?
- google-apps-script - 使用“Else If”将数据输入特定列的最后一行
- python - 如何正确地将变量插入到python中的json键中
- javascript - GraphQL 无法为“非空字段”返回“空”
- r - R - 获取由不同数据框中的值定义的一组列中的最高/最低值
- vb.net - 如何使用 VB.NET 在 Access Query 中保存、编辑和创建新记录
- sql - 运行存储过程后如何保留临时表?
- javascript - Angular - 如何将路由功能传递给子组件
- reactjs - useEffect 的一致返回
- python - pyenv .zshrc 在新设置上产生错误