使用laravel和vue.js制作一個(gè)上傳圖片的組件

首先 在./components/common/創(chuàng)建組件的vue文件imageUpload.vue
然后記得在components下的app.js注冊(cè)組件

Vue.component('image-upload', require('./components/common/imageUpload.vue'))

image-upload就是組件名

imageUpload.vue 內(nèi)容為
<template>
<div class="columns">
<div class="column is-2">
<div class="field-label is-normal">
<label class="label">上傳圖片</label>
</div>
</div>
<div class="column is-10">
<div class="field-body">
<div class="field">
<div class="control">
<input type="hidden" name="uploadImg" :value="imagePath">
<input id="iphoneOpenCameraInput" class="is-hidden" type="file" accept="image/*" @change="uploadImg" ref="isUploadImg">
![](imgUrl)
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
props: { imagePath:String, imgUrl:String, uploadUrl:String },
methods: {
uploadImg(){
var self= this
var file=this.$refs.isUploadImg.files[0]
console.log(file)
var reader = new FileReader()
reader.readAsDataURL(file)
reader.onloadend = function () {
self.imgUrl=this.result
var postData={
imgUrl:this.result
}
self.$axios.post(self.uploadUrl,postData).then((response) => {
self.imagePath = response.data['imgPath'] }, (error) => {
console.log(error)
})
}
},
openUploadImages () {
this.$refs.isUploadImg.click();
},
}
}
</script>

注:上傳圖片的默認(rèn)圖片我這里放在了public下的image下,可根據(jù)自己需要修改
這樣在引入時(shí)候直接調(diào)用<image-upload></image-upload>就可以使用上傳組件了

寫(xiě)好了組件 我們來(lái)看看怎么使用laravel去寫(xiě)上傳部分的后端代碼
首先要在public文件下創(chuàng)建/upload/images的路徑存放圖片
下面看看后端的代碼:
public function imgUpload(Request $req) {
$uploadImg=$req->imgUrl;
$data=array();
$data['imagesPath']='';
$data['error']='';
$data['status']=1;
if($uploadImg){
$base64_body = substr(strstr($uploadImg, ','), 1);
$img = base64_decode($base64_body);
$path='upload/images/';
if(!file_exists($path)){
$data['error']='文件目錄不存在';
$data['status']=0;
}
$filename = $path. date('YmdHis', time()).rand(100000,999999) . '.jpg';
if (file_put_contents($filename, $img, FILE_APPEND)) {
$data['imagesPath']=url($filename);
} else {
fopen($filename, 'a+');
unlink($filename);
$data['error']='文件獲取失敗!';
$data['status']=0;
}
}
return $data;
}
返回的data就是我們的文件路徑了 你可以在這里上傳路徑到數(shù)據(jù)庫(kù)

最后在路由文件里面注冊(cè)這個(gè)方法 就可以在前端異步請(qǐng)求了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 來(lái)源:github.com Vue.js開(kāi)源項(xiàng)目速查表:https://www.ctolib.com/cheats...
    zhangtaiwei閱讀 11,924評(píng)論 1 159
  • 轉(zhuǎn)載 :OpenDiggawesome-github-vue 是由OpenDigg整理并維護(hù)的Vue相關(guān)開(kāi)源項(xiàng)目庫(kù)...
    果汁密碼閱讀 23,405評(píng)論 8 124
  • UI組件element ★11612 - 餓了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于...
    董董董董董董董董董大笨蛋閱讀 8,797評(píng)論 6 123
  • 懵懵懂懂,跌跌撞撞,一腳踏進(jìn)來(lái),抬頭四顧,訝然,欣喜,又惴惴不安,原來(lái)你在這里呀!2017.8.6號(hào)于孟州。
    七月彭鵬閱讀 234評(píng)論 0 0
  • 氣節(jié)操守,是做人的標(biāo)準(zhǔn),是檢驗(yàn)靈魂的試金石。具有高尚節(jié)操者,誠(chéng)信無(wú)欺,見(jiàn)義勇為,甚至舍生取義。他們能做到:“富貴不...
    有訶卟薖閱讀 496評(píng)論 0 0

友情鏈接更多精彩內(nèi)容