Javascript 从Laravel中的Vue检索文件
我使用Vue将文件从前端上传到Laravel后端 使用以下代码段上载:Javascript 从Laravel中的Vue检索文件,javascript,laravel,file-upload,vue.js,Javascript,Laravel,File Upload,Vue.js,我使用Vue将文件从前端上传到Laravel后端 使用以下代码段上载: addPost() { axios.post('api/submitPropertyPhoto?token=' + this.token, this.postFormData) .then(r => { console.log(r) }) .catch(e => { console.log(e) } }, uploadFieldChange(e) { for(let key in e.target.fi
addPost() {
axios.post('api/submitPropertyPhoto?token=' + this.token, this.postFormData)
.then(r => {
console.log(r)
})
.catch(e => {
console.log(e)
}
},
uploadFieldChange(e) {
for(let key in e.target.files) {
this.postFormData.append('images[]', e.target.files[key])
}
}
当我想使用普通的Laravel请求文件助手方法请求文件时,它什么也不返回,但当我使用dd($request->files)
时,它返回下面的详细信息
FileBag {#68
#parameters: array:1 [
"images" => array:1 [
0 => array:1 [
"name" => UploadedFile {#45
-test: false
-originalName: "error.JPG"
-mimeType: "image/jpeg"
-size: 21806
-error: 0
path: "C:\xampp\tmp"
filename: "php639F.tmp"
basename: "php639F.tmp"
pathname: "C:\xampp\tmp\php639F.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php639F.tmp"
aTime: 2018-05-17 04:26:29
mTime: 2018-05-17 04:26:29
cTime: 2018-05-17 04:26:29
inode: 0
size: 21806
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\xampp\tmp\php639F.tmp"
}
]
]
]
}
基本上,我想要实现的是将文件保存在光盘中,并将文件名保存在数据库中。在控制器上尝试以下操作:
$urls = collect();
foreach ($request->images as $image) {
$path = $image->store('images');
// Store $path to your data base
$urls->merge(Storage::url($path));
}
return response($urls);
然后在vue上,您可以获得URL:
addPost() {
axios.post('api/submitPropertyPhoto?token=' + this.token,this.postFormData)
.then(r => {
console.log(r.data); // You should get a json of urls
})
.catch(e => {
console.log(e)
}
},您正在通过POST请求发送文件。难道你不能直接链接到POST请求将文件保存到服务器上吗?