当前位置:   article > 正文

uni-app上传图片和文件_uniapp 上传excel

uniapp 上传excel

如图所示:

上传图片,使用的是uni.chooseImage这个官方api,count 数量根据自己的需求来,我们是最多只能上传9张

  1. uploadImgEvent(){
  2. uni.chooseImage({
  3. count: 10 - this.uploadImgsList.length,
  4. success: (res) => {
  5. this.uploadImgsList.unshift(...res.tempFiles.map(item => ({
  6. picturePath: item.path
  7. })));
  8. if(this.uploadImgsList.length == 10) this.uploadImgsList.pop();
  9. }
  10. });
  11. },
  1. uploads(){
  2. const _this = this;
  3. return Promise.all(_this.uploadImgsList.map(item=>_this.uploadImage(item.picturePath))).then(res=>{
  4. return res.map(item=>({
  5. fileName:item.fileName,
  6. filePath:item.url
  7. }))
  8. })
  9. },
  10. uploadImage(url) {
  11. return new Promise(async (resolve, reject) => {
  12. uni.uploadFile({
  13. url: await getUploadUrl(), //后台地址
  14. filePath: url,
  15. name: 'file',
  16. success: (uploadFileRes) => {
  17. resolve(JSON.parse(uploadFileRes.data));
  18. }
  19. })
  20. })
  21. },

提交给后代的数据

  1. //提交
  2. async submitFlood(){
  3. let photoList = await this.uploads();
  4. }

 打印photoList如图:

 上传文件使用的LFile 这个插件 https://ext.dcloud.net.cn/plugin?id=4109

根据官网案例来

  1. //上传附件
  2. uploadFile() {
  3. const that = this;
  4. if(that.imgUploadList.length >= 9){
  5. this.$mHelper.toast('最多上传9张')
  6. return;
  7. }
  8. that.$refs.lFile.upload({
  9. // #ifdef APP-PLUS
  10. currentWebview: that.$mp.page.$getAppWebview(),
  11. // #endif
  12. url: 'xxxxxx', //你的上传附件接口地址
  13. name: 'file'
  14. },
  15. });
  16. },
  1. //上传成功
  2. upSuccess(res) {
  3. let url = res.root.url;
  4. let name = res.root.originalName;
  5. let fileType = this.isFileType(res.root.type);
  6. let size = res.root.size;
  7. if(fileType == 'image'){
  8. this.imgUploadList.push({url,name,fileType,size});
  9. }else{
  10. this.fileUploadList.push({url,name,fileType,size})
  11. }
  12. },
  13. //根据文件后缀名来判断,展示对应的图标
  14. isImage(type){
  15. return ['png','jpg','jpeg','gif','svg'].includes(type.toLowerCase());
  16. },
  17. isDocx(type){
  18. return ['doc','docx'].includes(type.toLowerCase());
  19. },
  20. isXsls(type){
  21. return ['xsls','xsl'].includes(type.toLowerCase());
  22. },
  23. isText(type){
  24. return ['text','txt'].includes(type.toLowerCase());
  25. },
  26. isFileType(type){
  27. if(this.isImage(type)) return 'image';
  28. if(this.isXsls(type)) return 'excel';
  29. if(type == 'pdf') return 'pdf';
  30. if(this.isDocx(type)) return 'word';
  31. if(this.isText(type)) return "text";
  32. // return "#icon-file-b--6";
  33. },
  34. //文件预览
  35. previewFile(item){
  36. uni.downloadFile({
  37. url: item.url,
  38. success: (res) => {
  39. let filePath = res.tempFilePath;
  40. uni.openDocument({
  41. filePath: filePath,
  42. success: (res) => {
  43. console.log('打开文档成功');
  44. }
  45. })
  46. }
  47. })
  48. },
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/571054
推荐阅读
相关标签
  

闽ICP备14008679号