首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用laravel 6显示Blob列的文档

使用laravel 6显示Blob列的文档
EN

Stack Overflow用户
提问于 2019-12-30 17:24:40
回答 2查看 796关注 0票数 0

我试图从Mysql中的Blob列中显示一个文档(pdf或图像)。

这是我的代码上传一个pdf或图像文件。

代码语言:javascript
运行
复制
    public function store(Request $request)
    {
        //
        Request()->validate([
            'userfile' => 'required|mimes:doc,docx,pdf,jpeg,png,jpg,gif,svg|max:2048',
            ]);

            $f = $request->file('userfile');
            $att = new \App\Image;
            $att->vno = $request->vno;
            $att->vtype = $request->vtype;
            $att->descrip = $request->docdesc;
            $att->fname = $f->getClientOriginalName();
            $att->imagefile = base64_encode(file_get_contents($f->getRealPath()));
            $att->mime = $f->getMimeType();
            $att->size = $f->getSize();
            $att->save();
            return response()->json(['success'=>$att->id]);
    }

若要检索和显示文档,请使用以下代码。

代码语言:javascript
运行
复制
    public function show($id)
    {
        //
        $doc = DB::table('images')->where('id',$id)->get();
        $tmpdoc = $doc[0]->imagefile;
        return Response::make($tmpdoc, 200, [
            'Content-Type' => $doc[0]->mime,
            'Content-Disposition' => 'inline; filename="'.$doc[0]->fname.'"'
        ]);
    }

但我得到一个空白页的情况下,一个图像文件和“未能加载pdf文件”的pdf文件。

更新

使用MySQL手动上传图像和PDF文档到WorkBench,并且该图像正在正确显示。所以我认为在文件上传部分有一些问题。

上传的HTML是

代码语言:javascript
运行
复制
<div class="col-sm-8">
  <input type="text" class="form-control" id="docdesc" name="docdesc" placeholder="Enter description for document" value="">
  <input type="file" id="filename" name="filename" class="form-control">
</div>
<div class="col-sm-2">
  <button type="button" class="btn btn-secondary" id="addDoc">Attach</button>
</div>

和ajax代码

代码语言:javascript
运行
复制
$('#addDoc').click(function (e) {
    // Save Image to Table
    $.ajaxSetup({
        headers: {
           'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    e.preventDefault();
    var fd = new FormData();
    fd.append('userfile', $('#filename')[0].files[0]);
    $.ajax({
        data: fd,
        url: "{{ route('images.store') }}",
        cache:false,
        processData: false,
        contentType: false,
        type: "POST",
        success: function (data) {
            $('#documentForm').trigger("reset");
        },
    });    
});
EN

回答 2

Stack Overflow用户

发布于 2019-12-30 18:03:08

您是否也可以尝试传递内容大小和以下标题:

代码语言:javascript
运行
复制
    return Response::make($tmpdoc, 200, [
        'Content-Type' => $doc[0]->mime,
        'Content-length' => strlen($tmpdoc),
        'Pragma'=>'private',
        'Cache-Control'=>'max-age=0',
        'Content-Disposition' => 'inline; filename="'.$doc[0]->fname.'"'
    ]);
票数 0
EN

Stack Overflow用户

发布于 2019-12-31 10:32:46

删除base64_encode()解决了这个问题。

变化

代码语言:javascript
运行
复制
$att->imagefile = base64_encode(file_get_contents($f->getRealPath()));

代码语言:javascript
运行
复制
$att->imagefile = file_get_contents($f->getRealPath());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59534580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档