dsds33222 2019-01-22 07:47
浏览 169

可以使用blade.php打开本地文件并显示内容吗? laravel

Currently I have a project with laravel 5.5 I have some text files stored in storage/public/* I have been thinking if there is a way to use laravel's blade and open the selected file directly instead of making a request to the backend to retrieve the content then display.

The idea is inside *.blade.php there will be a button to click, then instead of making request to the backend, can we just open the text file then show the content?

let's say for example, I do have html code like

<input type='hidden' value='filePath' name=path>
<a href="">text file name</a>

when the anchor is clicked, usually it would be making a request to the backend open the file, get the content, pass it back to the front. But what I am thinking is, since the frontend knows where the path is, name of the file. Is it possible to do it at front end? This way no requests need to be done faster too.

But I do not have an idea how to even start it.

Does someone has any suggestions?

Thanks in advance for any help.

  • 写回答

1条回答 默认 最新

  • du512053619 2019-01-23 23:46
    关注

    Assuming your files are accessible to the person viewing the web page, you can use file URLS (or just type in the URL in the blade file?).

    <a href="http://public/fixed/path/to/file">Title</a>
    

    Or, if the files are pre-determined when the blade file is generated then do something like this in your controller:

    use Illuminate\Support\Facades\Storage;
    
    public function index()
    {
        return view('view.name', [
            'url' => Storage::url('file.jpg')
        ]);
    }
    

    And in your Blade file:

    <a href="{{ $url }}">Title</a>
    

    If you consider the blade file the front-end, then remember it's not hosted on the server once the user is viewing it. You'll need to do something like the above to allow blade to generate the right file URLs (unless you're just talking about static, hosted files)

    But, if your reference to back-end and front-end is about where should the logic go, then the answer is "the back end". The point of the MVC pattern is to put the right bits of logic in the right places, so the Blade file (representing V for View in MVC) should only be concerned with presentation, not digging up files and generating URLs.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?