Laravel + Image Intervention:强制下载未保存的文件 [英] Laravel + Image Intervention : Force download of file that is not saved

查看:183
本文介绍了Laravel + Image Intervention:强制下载未保存的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简单地上传文件,调整它们的大小,然后为每个上传的文件强制下载.我不想保存文件.

调整大小等工作正常,但是我无法强制强制下载新文件.

$content = $image->stream('jpg');

return response()->download($content, $name);

显示的摘录结果为

is_file()期望参数1为有效路径,并给出字符串

最可能是因为$ content不是路径,而是实际数据.

是否可以在不先保存的情况下强制执行下载?

解决方案

尝试一下:

$url = "https://cdn.psychologytoday.com/sites/default/files/blogs/38/2008/12/2598-75772.jpg";
$name = 'happy.jpg';

$image = Image::make($url)->encode('jpg');
$headers = [
    'Content-Type' => 'image/jpeg',
    'Content-Disposition' => 'attachment; filename='. $name,
];
return response()->stream(function() use ($image) {
    echo $image;
}, 200, $headers);

这就是我们正在做的.我们正在使用干预图像将图像编码为适合我们的正确文件类型格式.然后,我们自己设置浏览器标题,以告知浏览器文件类型,并希望浏览器将其视为可下载附件.最后,我们使用Laravel的stream()方法返回响应并使用闭包.我们告诉闭包它可以使用use ($image)访问图像的编码数据,然后我们只需将原始数据回显到页面上即可.从那里,我们告诉浏览器HTTP代码为200(确定),然后将其标头告知浏览器该怎么做.

您的问题是Laravel方便的download()方法期望它是文件系统上的文件.因此,我们必须对其进行流传输并手动设置标题.

您将需要编写一些其他代码来处理Intervention使用的encode()方法以及返回的Content-Type的不同文件扩展名.您可以在此处找到列表.

我希望这会有所帮助.

更高版本的Laravel在响应对象上引入了deleteFileAfterSend()方法.如果可以将文件临时保存到服务器上,可以这样做:

return response()->download($path)->deleteFileAfterSend();

I want to simply upload files , resize them and then force a download for every uploaded file. I do not want to save the files.

Resizing etc. works fine, however I cannot manage to force the download of the new file.

$content = $image->stream('jpg');

return response()->download($content, $name);

The shown snippet results in

is_file() expects parameter 1 to be a valid path, string given

Most probably because $content is not a path but the actual data.

Is there a way to enforce the download without saving it first?

解决方案

Try this:

$url = "https://cdn.psychologytoday.com/sites/default/files/blogs/38/2008/12/2598-75772.jpg";
$name = 'happy.jpg';

$image = Image::make($url)->encode('jpg');
$headers = [
    'Content-Type' => 'image/jpeg',
    'Content-Disposition' => 'attachment; filename='. $name,
];
return response()->stream(function() use ($image) {
    echo $image;
}, 200, $headers);

Here's what we're doing. We're using Intervention Image to encode that image into the correct file type format for us. Then we're setting the browser headers ourselves to tell the browser what type of file it is and that we want the browser to treat it as a downloadable attachment. Lastly, we return the response using Laravel's stream() method and use a closure. We tell the closure it has access to the encoded data of the image with use ($image) and then we simply echo that raw data onto the page. From there we tell the browser the HTTP code is 200 (OK), and then give it our headers to tell the browser what to do.

Your problem was that Laravel's handy download() method is expecting it to be a file on the file system. So we have to stream it and manually set the headers ourselves.

You will need to write some additional code to handle different file extensions for the encode() method Intervention uses as well as the Content-Type returned. You can find a list of all available mime types here.

I hope this helps.

Edit: Later versions of Laravel introduced the deleteFileAfterSend() method on the response object. If you are okay with temporarily saving the file to the server you can do this:

return response()->download($path)->deleteFileAfterSend();

这篇关于Laravel + Image Intervention:强制下载未保存的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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