首页 > 移动平台 > 详细

uniapp 图片上传封装

时间:2021-05-28 11:22:48      阅读:138      评论:0      收藏:0      [点我收藏+]

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)
			})         
})               

  


  

 

uniapp 图片上传封装

原文:https://www.cnblogs.com/geki/p/14820868.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!