dongshi9407 2019-02-03 15:12
浏览 551

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 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:

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

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

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


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条回答 默认 最新


      相关推荐 更多相似问题


      • ¥15 如何关闭soui dummy wdn 进程
      • ¥20 含有jar文件的文件夹用tar -zcvf 命令压缩后要怎么还原回去?
      • ¥15 天宝TBC软件出现报错怎么解决
      • ¥15 micropython 从1开始切片报错
      • ¥15 请问该怎么做才能将文字说明和二维码一起作为png图片一起下载
      • ¥15 r语言 混频数据回归 midas_r
      • ¥15 人脸识recogizer.predict返回值id报错,KeyError,详情如下
      • ¥15 geowebcache部署失败
      • ¥20 关于adaboost+haar特征实现人脸检测过程的一些细节问题
      • ¥15 csv文件增加一列 为csv文件的名字