我不知道如何获取数据库文件附件,该文件的附件是真实文件名。
我的模型有很多文件附件(很多attachOne),使用
链接到它们没有问题。import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MyContent {
anchors.centerIn: parent
}
}
我要做的是使用真实文件名下载这些文件。
我尝试在我的布局中定义ajax事件处理程序,如下所示:
<a href="{{ model.myfile.path }}" target="_blank">{{ model.myfile.filename }}</a>
和
function onDonwload()
{
$path = post('path');
$name = post('name');
// Storage::exists('uploads/public/5ce/28c/3aa/5ce27c3aae590316657518.pdf'); => OK
// Storage::exists($path); =>OK
$path = storage_path().'/app/'. $path;
return Response::download( $path, $name);
}
没有丢失文件的错误,但使浏览器冻结并显示一条警告,提示“网页会使浏览器变慢,您想做什么?”。
我错过了重点吗?
答案 0 :(得分:0)
您应该添加单独的页面来下载文件,Ajax不能帮助您下载文件(也许可以,但是处理过程并不复杂且漫长)
- 在
/file-download/:id
处创建页面,您可以在此处指定任何url wit参数:id
,并给它命名为file-download
,也可以给您喜欢的演示名称,我使用了该名称。
- 在“页面HTML”部分将为空白,然后在“页面代码”部分添加此代码。在这里,您还可以检查其他安全检查,例如用户已登录或文件是否属于相关用户。现在,我只是检查文件是否与特定的Modal相关。
function onStart() {
$fileId = $this->param('id');
$file = \System\Models\File::find($fileId);
// for security please check attachment_type == your model with namespace
// only then lat use download file other wise user can download all the files by just passing id
if($file && $file->attachment_type == 'Backend\Models\User') { // here add your relation model name space for comparison
$file->output('attachment');
}
else {
echo "Unauthorised.";
}
exit();
}
- 正在生成链接,请用您创建的页面替换页面名称。
<a href="{{ 'file-download'|page({'id': model.myfile.id}) }}" >{{ model.myfile.filename }}</a>
现在,当您单击链接文件时,应使用原始名称下载文件,如果文件ID无效,则应显示消息Unauthorised.
。
如有任何疑问,请发表评论。