简体   繁体   English

在Octobercms中,如何使用AJAX下载文件而不进行页面引用?

[英]How to use AJAX to download files without page referesh in Octobercms?

I am new to laravel and october cms. 我是laravel和10月cms的新手。 I want to add a button to allow users download a PDF file, 'a.pdf', without directing to a new page. 我想添加一个按钮,以允许用户下载PDF文件'a.pdf',而无需定向到新页面。 I know that I have to use AJAX and send a download http response in order to make browsers show a 'Save As...' dialog box. 我知道我必须使用AJAX并发送下载http响应,以使浏览器显示“另存为...”对话框。 So far this is what I have accomplished: 到目前为止,这是我已经完成的工作:

title = "Sandbox"
url = "/test"
layout = "default"
==
<?php
function onDownload()
{
    $pathToFile = Url::to("/storage/app/media/a.pdf");
    $fileName = "download.pdf";
    $headers = [
        'HTTP/1.1 200 OK',
        'Pragma: public',
        'Content-Type: application/pdf'
    ];
    return Response::download($pathToFile, $fileName, $headers);
}
?>
==
<div class="container">
    <form class="form-inline" data-request="onDownload">
        <button type="submit" class="btn btn-primary" data-attach-loading>Download</button>
    </form>
</div>

I get "The file " http://localhost/october/storage/app/media/a.pdf " does not exist" error using above code. 使用上述代码,我收到“文件” http://localhost/october/storage/app/media/a.pdf “不存在”错误。

What am I doing wrong? 我究竟做错了什么?

Please change the header into associative array, 请将标头更改为关联数组,

$headers = array(
                 'Content-Type'=>'application/pdf',
           );

For any who may find this useful ( This works for files uploaded via media manager ). 对于可能会觉得有用的任何人( 这适用于通过媒体管理器上传的文件 )。 Got this working by adding the AJAX handler in my layout code php section. 通过在我的布局代码php部分添加AJAX处理程序来完成此工作。

function onDownloadButtonClicked(){

    $pathToFile = base_path('storage/app/media/path/actual filename here');
    $fileName = "filename shown on download here";
    $headers = [
        'HTTP/1.1 200 OK',
        'Pragma: public',
        'Content-Type: application/pdf'
    ];
    return Response::download($pathToFile, $fileName, $headers);
}

Added this on the pages and partials 在页面和局部上添加了此

{{ form_open({ request: 'onDownloadButtonClicked' }) }}
<button type="submit" class="tg-btn tg-btnicon" data-attach-loading>
    <i class="fa fa-file-pdf-o"></i> Download Here
</button>
{{ form_close() }}

Update : For file uploaded via the backend using a plugin Octobercms generates dynamic links. 更新 :对于使用插件Octobercms通过后端上传的文件,它会生成动态链接。 To download the files just create a frontend component. 要下载文件,只需创建一个前端组件。 Follow easy tutorial Watch + learn to create a simple plugin model for backend uploading and Mastering components for a barebones frontend download component. 遵循简单的教程Watch +学习创建一个简单的插件模型,用于后端上传和母带组件的准系统前端下载组件。 Once the file details are acquired a simple href will do for file download. 获取文件详细信息后,将使用简单的href进行文件下载。

<a href="{{ post.attachment.path }}"
            download="{{ post.attachment.file_name }}"><i class="fa fa-download"></i> &nbsp Download File</a>

The names posts and attachment could be anything depending plugin model form design. 名称插件和附件可以是任何东西,具体取决于插件模型的形式设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM