我试图将图片上传到存储空间。我目前有这个对象,但似乎很不幸保存在app / storage
上Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => lamborghini_veneno_art_minimalism_99945_1366x768.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 117303
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[hashName:protected] =>
[pathName:SplFileInfo:private] => C:\xampp\tmp\php82F7.tmp
[fileName:SplFileInfo:private] => php82F7.tmp
)
这是我的代码。
在profile.vue中,我从输入文件中点击了事件。
onFilePicked(event){
const files = event.target.files
const data = new FormData();
data.append('avatar', files[0]);
this.$store.dispatch('uploadImage_profile',data)
.then(response=>{
})
.catch(error=>{
})
}
然后使用axios发送
axios({
url: '/prod/api/uploadImage_profile',
method: 'post',
data: obj
})
.then(response=>{
if(response.status == 200){
resolve(response.data)
}
})
.catch(error=>{
if(error.response){
reject(error.response.data);
}
})
我的Controller.php
public function uploadImage_profile(Request $request){
$response = [];
//var_dump($request->file('avatar'));
//$path = $request->file('avatar')->store('avatars');
if($request->hasFile('avatar')){
$file = $request->file('avatar');
Storage::put('file.jpg', $file);
}
return response()->json($response);
}
答案 0 :(得分:2)
您需要在axios post请求中将<=
设置为Content-Type
:
multipart/form-data
答案 1 :(得分:1)
尝试这种方式:
$file = $request->file('avatar');
$destinationPath = storage_path() . '/folder'; // directory under which you want to store the file
if( $file ->move( $destinationPath, $file ) )
{
// image moved successfuly
}
else
{
// fail to move image
}