dongshi9407 2019-02-03 15:12
浏览 565
已采纳

Laravel在Ajax模式中调用response() - > file($ pathToFile)

Since I dont like the default way a file is presented to a user when "return response()->file($pathToFile);" is called I wanted to make a Version in which a file isnt direcly displayed in the current Tab of the Website but rather in a popup Modal - Kind of like how its done when you preview a file in Dropbox.

So what I did is add a button to my view that calls for my Route "preview_modal" This Route then calls my "preview_modal" method in my FileController. This Method returns the requested File in the Modal.

But sadly when the Mimetype of the requested File is not a "text/*" type of File I get this kind of Text presented to me in my Modal:

�PNG  IHDRq�:sRGB���gAMA���a pHYs���o�d   *IDATx^��r�6��~ Ob~��jn9�-��.Q�v˖-��ʥ��`ip���L��d��?�E����'0�0�M�afİ�3Ì6q�a��&�03b���aF����L&�V���H|�̏t[���kȲ�01����,�|�r�b��`����㙮�|:��/���� r/[X�>`�]��KH���5�Oޟa� ��N�A�|��KX��2�:γ�d ٢��,�

In this case it was a PNG that was beeing opened. This Text was just a little example.

I have no idea what im looking at If anyone knows what this is i would greatly appreciate it. The "return response()->file()" works fine for the same file if it isnt called inside the ajax modal.

Button:

<button data-path="{{ route('files.preview_modal', $file) }}" class="button is-info load-ajax-modal" role="button" data-toggle="modal" data-target="#dynamic-modal">Preview</button>

Ajax Call:

$.ajaxSetup({
    headers: {
        'X-CSRF-Token': $('meta[name="_token"]').attr('content')
    }
});

$('.load-ajax-modal').click(function () {
    $.ajax({
        type: 'GET',
        url: $(this).data('path'),

        success: function (result) {
            $('#dynamic-modal div.modal-body').html(result);
        }
    });
});

Route:

Route::get('preview_modal/{file}', 'FileController@preview_modal')->name('files.preview_modal');

preview_modal Method:

public function preview_modal(File $file)
{
  return response()->file(storage_path("$file->path"));
}
  • 写回答

4条回答 默认 最新

  • douqiao1997 2019-02-03 18:29
    关注

    Try final these 2 solutions

    1. storage_path("$file->path") remove double quotes: storage_path($file->path)

    because you are using variable not the string

    1. may be you should convert you image to base64.

      $path = storage_path($file->path); $type = pathinfo($path, PATHINFO_EXTENSION); $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

    and then use it in html enjoy the day mate :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵