我想在上传时从docx文件中提取内容但我收到此错误:类PhpOffice \ PhpWord \ PhpWord的对象无法转换为字符串 任何解决方案请告诉我?
public function store(Request $request){
$this->validate($request, [
'file'=>'required|mimes:doc,pdf,docx'
]);
if($request->hasFile('file')) {
$file= $request->file('file');
$fileName = time() .'.'.$file->getClientOriginalExtension();
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$obj = \PhpOffice\PhpWord\IOFactory::createReader($phpWord, 'Word2007');
$obj->load($fileName);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($obj, 'Word2007');
try {
$objWriter->save(storage_path('my.docx'));
} catch (Exception $e) {
}
return response()->download(storage_path('my.docx'));
}
}
答案 0 :(得分:0)
<强>答案:强>
public function store(Request $request){
$this->validate($request, [
'file'=>'required|mimes:docx,doc'
]);
$fatwa = new Fatwa;
if($request->hasFile('file')) {
$file= $request->file('file');
$objReader= \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
$phpWord= $objReader->load($file);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
try {
$objWriter->save(storage_path('my.docx'));
} catch (Exception $e) {
}
return response()->download(storage_path('my.docx'));
}
}