uniapp 圖片上傳封裝



serve.js//和請求一塊封裝
BASE_URL = 'https:test.com/api'//統一請求地址
export const uploadFile = (options) => { return new Promise((resolve, reject) => { console.log('options', options) let header = {} header['Content-Type'] = "multipart/form-data" try { const value = uni.getStorageSync('token');//取存本地的token if (value) { header['token'] = value } } catch (e) { // error } uni.uploadFile({ url: baseUrl + options.url, name: 'file', filePath: options.filePath.img, header, success: (res) => { // return res resolve(res) //如果請求成功,調用resolve返回數據 }, }) }) }

upload_serve.js
import {uploadFile} from "./serve.js"
export const upload = (data) => {
	return uploadFile({
		url: "/test",//請求后綴
		filePath: data,
		header: {
			'Content-Type': 'multipart/form-data'
		}
	})
}

//調用upload_server.js
import {upload} from upload_serve.js
uni.chooseImage({
		count: 1, //默認9
		sizeType: ['original', 'compressed'], 
		sourceType: ['album'], //從相冊選擇
                success: function (chooseImageRes) {
			let img=chooseImageRes.tempFilePaths[0]
               upload({img}).then(res=>{ //直接通過uni.chooseImage獲取地址傳給后端
			let resData=JSON.parse(res.data)
			console.log(resData)
			})         
})               

  


  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM