Vant Uploaderとaxiosは画像をアップロードしてバックグラウンドにurlを取得します
8072 ワード
Vant Uploaderとaxiosは画像をアップロードしてバックグラウンドにurlを取得します
htmlコード:
jsコードをアップロードするには:
htmlコード:
<van-uploader :after-read="afterRead" :upload-text="' '" :before-read="beforeRead" />
jsコードをアップロードするには:
/**
*
*/
afterRead (file) {
this.uploadImg(file.file)
},
/**
*
*/
beforeRead (file) {
if (file.type !== 'image/jpeg' && file.type !== 'image/png') {
Toast(' jpg/png ');
return false;
}
let isLt1M = file.size / 1024 / 1024 <= 1
if (!isLt1M) {
Toast(' 1M ');
return false
}
return true;
},
/**
*
*/
uploadImg (file) {
// form
let formdata1 = new FormData();
// append form , append
// formdata1.append('file',file);
formdata1.append('file', file, file.name);
//
let config = {
headers:{
'Content-Type':'multipart/form-data'
}
};
//this.axios main.js vue
const axiosAjax = this.axios.create({
timeout: 1000 * 60, //
withCredentials: true // cookie
});
axiosAjax.post('url',formdata1,config).then((res)=>{
// url
console.log(res.data);
//res
}).catch(() => {
})
}