基于uni-app实现图片上传JS插件

 5766

使用前先new 一下

所有方法均返回 promise 对象 可使用then() 写后续业务 或 使用 async await

服务端返回示例

  1. {
  2.     "code":0,
  3.     "msg":"上传成功",
  4.     "data":"http://www.test.com/uploads/20190227/f2824d2d4dc38f30699f816226b4a578.jpg"
  5. }

直接上源码

  1. /*
  2.     2019-02-27
  3.     lane
  4.     封装 uni-app 图片上传功能
  5.     使用前先new 一下
  6.     所有方法均返回 promise 对象 可使用then() 写后续业务 或 使用 async await
  7.     服务端返回示例
  8.     {
  9.         "code":0,
  10.         "msg":"上传成功",
  11.         "data":"http://www.test.com/uploads/20190227/f2824d2d4dc38f30699f816226b4a578.jpg"
  12.     }
  13.     choose  选择图片
  14.         参数 num 为要选择的图片数量
  15.     upload_one 上传一张图片
  16.         参数 path  选择成功后返回的 缓存文件图片路径
  17.     upload  上传多张图片
  18.         参数 path_arr 选择图片成功后 返回的图片路径数组
  19.     choose_and_upload  选择图片并上传
  20.         参数 num 为要选择的图片数量
  21. */
  22. // 引入配置信息或者自己创建个 config 对象
  23. import config from "../config.js";let config = {
  24.     // 上传图片的API
  25.     upload_img_url:'http://uni_upload.gek6.com/index.php/index/upload'
  26. }
  27. class Uploader {
  28.     constructor() {
  29.     }
  30.     choose(num) {       return new Promise((resolve, reject) => {
  31.             uni.chooseImage({
  32.                 count: num,
  33.                 success(res) {
  34.                     // console.log(res);
  35.                     // 缓存文件路径
  36.                     resolve(res.tempFilePaths)
  37.                 },
  38.                 fail(err) {
  39.                     console.log(err)
  40.                     reject(err)
  41.                 }
  42.             })
  43.         }) 
  44.     }
  45.     upload_one(path) {      return new Promise((resolve, reject) => {
  46.             uni.showLoading({
  47.                 title:'上传中'
  48.             })
  49.             uni.uploadFile({
  50.                 url: config.upload_img_url, //仅为示例,非真实的接口地址
  51.                 filePath: path,
  52.                 name: 'file',
  53.                 success: (uploadFileRes) => {
  54.                     if("string"===typeof uploadFileRes.data){
  55.                         resolve(JSON.parse(uploadFileRes.data).data)
  56.                     }else{
  57.                         resolve( uploadFileRes.data.data )
  58.                     }
  59.                 },
  60.                 complete() {
  61.                     uni.hideLoading()
  62.                 }
  63.             });
  64.         })
  65.     }
  66.     upload(path_arr) {
  67.         let num = path_arr.length;
  68.         return new Promise(async (resolve, reject) => {
  69.             let img_urls = []
  70.             for (let i = 0; i < num; i++) {
  71.                 let img_url = await this.upload_one(path_arr[i]);
  72.                 console.log(img_url)
  73.                 img_urls.push(img_url)
  74.             };
  75.             console.log("全部上传成功")
  76.             resolve(img_urls)
  77.         }) 
  78.     }
  79.     choose_and_upload(num) {
  80.         return new Promise(async (resolve, reject) => {
  81.             let path_arr = await this.choose(num);
  82.             let img_urls = await this.upload(path_arr);
  83.             resolve(img_urls);
  84.         }) 
  85.     }
  86. }export default Uploader;

choose 选择图片

  1. 参数 num 为要选择的图片数量
  2. 返回 图片缓存路径 数组

upload_one 上传一张图片

  1. 参数 path  选择成功后返回 远程图片路径

upload 上传多张图片

  1. 参数 path_arr 选择图片成功后 返回远程图片路径数组

choose_and_upload 选择图片并上传

  1. 参数 num 为要选择的图片数量 返回 图片缓存路径 数组



本文网址:https://www.zztuku.com/index.php/detail-7968.html
站长图库 - 基于uni-app实现图片上传JS插件
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐