gpt4 book ai didi

mysql - 使用 laravel 6 显示 Blob 列中的文档

转载 作者:行者123 更新时间:2023-11-29 15:18:19 25 4
gpt4 key购买 nike

我正在尝试从 Mysql 中的 Blob 列显示文档(pdf 或图像)。

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

    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]);
}

要检索并显示我正在使用以下代码的文档。

    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 文档”。

<小时/>

更新

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

要上传的 HTML 是

<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代码

$('#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");
},
});
});

最佳答案

您可以尝试传递内容大小以及下面的标题吗:

    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.'"'
]);

关于mysql - 使用 laravel 6 显示 Blob 列中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com