vue中實現圖片丶檔案上傳
阿新 • • 發佈:2019-01-05
html頁面
<input type="file" value="" id="file" @change='onUpload'>//注意不能帶括號
js程式碼
methods: {
//上傳圖片
onUpload(e){
var formData = new FormData();
f ormData.append('file', e.target.files[0]);
formData.append('type', 'test');
$.ajax({
url: '/ShopApi/util/upload.weixun'
type: 'POST',
dataType: 'json',
cache: false,
data: formData,
processData: false,
contentType: false,
success: (res) => {
if (res.code === 200) {
var img_tpl ='<div class="picture" style="width:108px;float:left;"><img id="preview" src="'+
$("#refund_img").after(img_tpl);
}
},
error: function(err) {
alert("網路錯誤");
}
});
}
}
圖片效果圖