dongtiao0279 2018-08-31 10:25
浏览 288
已采纳

Laravel - pdf文件的响应返回奇怪的文本而不是在浏览器中显示pdf

after two days of reading and looking for solutions and other possible ways I finally encouraged myself to ask for help here. A lot of people have asked similar questions, I have used ALL of their solutions and a lot of them helped, up to this point. I'm completely stuck at this point.

To my problem: I want to upload pdf files to the storage directory and afterwards make them accessable for users clicking on a simple button (using jquery ajax post request). Uploading works fine and the PDF ends up in the right directory, as the title states, returning the file works aswell, but instead of displaying the pdf in the browser, it simply returns a for most parts weirdly encoded string starting with the following:

%PDF-1.3 %âãÏÓ

My jQuery Code:

$('input.btn-pdf').click(function () {
    event.preventDefault();
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax({
        url: '/big-picture/getPDF',
        type: 'POST',
        data: {'filename':$(this).val()},
        success: function (data) {
        }
    });
});

After clicking the button as a user the PDF's shouldn't be downloaded immediately, therefore I consulted the Laravel (5.6) Documentation and used the appropriate response method as follows in my controller:

 public function getPDF(Request $request)
{
    $headers = [
        'Access-Control-Allow-Origin' => '*',
        'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, PATCH, DELETE',
        'Access-Control-Allow-Headers' => 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers',
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline'
        ];
    return response()->file(storage_path("app\pdfs\\".$request->filename), $headers);
}

As I said, it finds the file and I don't get any errors (status code 200). I couldn't find any information of me having to do anything in the view or in the success area of my jquery ajax call after return the file as a response in order to display it correctly. I also checked the filetype of the file before returning it and it correctly said 'application/pdf'. I have gotten a lot of error messages first, then I found out that I have to use the right headers to make it work, which I did. Now it should work, but it doesn't.

This is the response header:

Accept-Ranges: none Access-Control-Allow-Headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, PATCH, DELETE Access-Control-Allow-Origin: * Cache-Control: public Connection: close Content-Disposition: inline Content-Length: 1650653 Content-Type: application/pdf Date: Fri, 31 Aug 2018 09:51:04 GMT Date: Fri, 31 Aug 2018 09:51:04 +0000 Host: localhost:8000 Last-Modified: Thu, 30 Aug 2018 09:30:39 GMT Set-Cookie: XSRF-TOKEN=aLongTokenHere; expires=Fri, 31-Aug-2018 11:51:04 GMT; Max-Age=7200; path=/ Set-Cookie: laravel_session=aLongSessionIdHere; expires=Fri, 31-Aug-2018 11:51:04 GMT; Max-Age=7200; path=/; httponly X-Powered-By: PHP/7.2.4

Thank you in advance. I'm not only interested in a solution for this problem, but if someone has a different idea of smoothly and intelligently storing PDF's for my particular use case and returning/displaying them I'm more than open to change my way of doing it.

All of you have a nice day.

  • 写回答

1条回答 默认 最新

  • doushichi3678 2018-09-08 12:55
    关注

    My Workaround:

    Instead of using the local storage facility of laravel, I will use Drop Box to store the PDF files and simply put the display links to the PDF's n my database, which will later be passed to my view.

    Not the solution I was hoping to end up with but it works. Maybe this is helpful for others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译