uni-app上传图片和文件
如图所示:

上传图片,使用的是uni.chooseImage这个官方api,count 数量根据自己的需求来,我们是最多只能上传9张
uploadImgEvent(){
uni.chooseImage({
count: 10 - this.uploadImgsList.length,
success: (res) => {
this.uploadImgsList.unshift(...res.tempFiles.map(item => ({
picturePath: item.path
})));
if(this.uploadImgsList.length == 10) this.uploadImgsList.pop();
}
});
},
uploads(){
const _this = this;
return Promise.all(_this.uploadImgsList.map(item=>_this.uploadImage(item.picturePath))).then(res=>{
return res.map(item=>({
fileName:item.fileName,
filePath:item.url
}))
})
},
uploadImage(url) {
return new Promise(async (resolve, reject) => {
uni.uploadFile({
url: await getUploadUrl(), //后台地址
filePath: url,
name: 'file',
success: (uploadFileRes) => {
resolve(JSON.parse(uploadFileRes.data));
}
})
})
},
提交给后代的数据
//提交
async submitFlood(){
let photoList = await this.uploads();
}
打印photoList如图:

上传文件使用的LFile 这个插件 https://ext.dcloud.net.cn/plugin?id=4109
根据官网案例来
//上传附件
uploadFile() {
const that = this;
if(that.imgUploadList.length >= 9){
this.$mHelper.toast('最多上传9张')
return;
}
that.$refs.lFile.upload({
// #ifdef APP-PLUS
currentWebview: that.$mp.page.$getAppWebview(),
// #endif
url: 'xxxxxx', //你的上传附件接口地址
name: 'file'
},
});
},
//上传成功
upSuccess(res) {
let url = res.root.url;
let name = res.root.originalName;
let fileType = this.isFileType(res.root.type);
let size = res.root.size;
if(fileType == 'image'){
this.imgUploadList.push({url,name,fileType,size});
}else{
this.fileUploadList.push({url,name,fileType,size})
}
},
//根据文件后缀名来判断,展示对应的图标
isImage(type){
return ['png','jpg','jpeg','gif','svg'].includes(type.toLowerCase());
},
isDocx(type){
return ['doc','docx'].includes(type.toLowerCase());
},
isXsls(type){
return ['xsls','xsl'].includes(type.toLowerCase());
},
isText(type){
return ['text','txt'].includes(type.toLowerCase());
},
isFileType(type){
if(this.isImage(type)) return 'image';
if(this.isXsls(type)) return 'excel';
if(type == 'pdf') return 'pdf';
if(this.isDocx(type)) return 'word';
if(this.isText(type)) return "text";
// return "#icon-file-b--6";
},
//文件预览
previewFile(item){
uni.downloadFile({
url: item.url,
success: (res) => {
let filePath = res.tempFilePath;
uni.openDocument({
filePath: filePath,
success: (res) => {
console.log('打开文档成功');
}
})
}
})
},
uni-app上传图片和文件的更多相关文章
- web app上传图片
很就很久以前,web app上传图片需要通过cordova插件,那时候好像还叫phonegap. 后来一个html标签就可以了 <input type="file" clas ...
- Ionic app 上传图片之webApi接口
App上传图片对应的webApi服务端是怎么处理的呢? using System; using System.Collections.Generic; using System.Diagnostics ...
- flask上传图片或者文件
在flask unittest自动化测试中如何上传图片或者文件 import StringIO picture = StringIO.StringIO(open('/home/admin/Pictur ...
- 查看真机的APP沙盒文件
1.Xcode --> window --> devices -->左边选择设备 右下边选择要查看的app 双击应用可查看目录 点击设置按钮,选 Download Container ...
- 用AJAX实现上传图片或者文件的方法
大家好,我是小C,最近在项目中用到ajax上传图片文件,本篇我们就说说ajax上传文件. 我们平时用到的AJAX,大部分都是传几个参数就可以了.简单说就是传几个字符串. $.ajax({ url: u ...
- sau交流学习社区--在element-ui中新建FormData对象组合上传图片和文件的文件对象,同时需要携带其他参数
今天有一个坑,同时要上传图片和文件,而且图片要展示缩略图,文件要展示列表. 我的思路是: 首先,只上传附件照片,这个直接看ele的官方例子就行,不仅仅上传附件照片,还同时上传其他参数. 然后,再做上传 ...
- python 全栈开发,Day129(玩具开机提示语,为多个玩具发送点播,聊天界面,app录音,app与服务器端文件传输,简单的对话)
一.玩具开机提示语 先下载github代码,下面的操作,都是基于这个版本来的! https://github.com/987334176/Intelligent_toy/archive/v1.2.zi ...
- django的所有app放在一个文件夹下便于管理
1.新建一个python Package,名字叫apps 2.拖拽以后的app到apps文件夹下,把Search for references勾选去掉,重要重要重要!!!! 3.右键点击apps文件夹 ...
- Facebook App 的头文件会有更多的收获
最近在看一些 App 架构相关的文章,也看了 Facebook 分享的两个不同时期的架构(2013 和 2014),于是就想一窥 Facebook App 的头文件,看看会不会有更多的收获,确实有,还 ...
- ajax 提交所有表单内容及上传图片(文件),以及单独上传某个图片(文件)
我以演示上传图片为例子: java代码如下(前端童鞋可以直接跳过看下面的html及js): package com.vatuu.web.action; import java.io.File; imp ...
随机推荐
- 苹果AppleMacOs系统Sonoma本地部署无内容审查(NSFW)大语言量化模型Causallm
最近Mac系统在运行大语言模型(LLMs)方面的性能已经得到了显著提升,尤其是随着苹果M系列芯片的不断迭代,本次我们在最新的MacOs系统Sonoma中本地部署无内容审查大语言量化模型Causallm ...
- RIPEMD算法:多功能哈希算法的瑰宝
一.RIPEMD算法的起源与历程 RIPEMD(RACE Integrity Primitives Evaluation Message Digest)算法是由欧洲研究项目RACE发起,由Hans D ...
- Spring 知识点
Spring Spring 是什么? Spring为了解决企业级应用的复杂性而创建,简化开发. 如下简化: 基于POJO的轻量级和最小侵入性编程: 以IoC(控制反转).DI(依赖注入)和面向接口实现 ...
- 编译器与Makefile
编译器与Makefile 目录 编译器与Makefile gcc/g++/clang clang gcc g++ 编译器过程 Makefile 什么是Makefile Makefile规则 变量 in ...
- django(web框架推导、简介、数据库初识)
一 web框架推导 1 软件开发架构 cs架构bs架构bs本质是也是cs # HTTP协议:无状态,无连接,基于请求,基于tcp/ip的应用层协议 # mysql:c/s架构,底层基于soket,自己 ...
- TagProvider 与 Enricher 丰富日志
TagProvider [LogProperties] 与 [LogPropertyIgnore] 如果用在DTO不存在任何问题,如果用在Domain实体上,可能有点混乱. 您可能不希望因日志记录问 ...
- ViewStub你真的了解吗
目录介绍 01.什么是ViewStub 02.ViewStub构造方法 03.inflate()方法解析 04.WeakReference使用 05.ViewStub为何无大小 06.ViewStub ...
- SqlSugar的几种连接方式
1.最简单的使用 public class DatabaseService { private static readonly Lazy<SqlSugarClient> _db = new ...
- MySQL函数GROUP_CONCAT()函数简介
一.数据需求按id分组然后把name用英文逗号分隔开 id name countryid age 1 曹操 1 56 2 刘备 2 47 3 孙权 3 38 4 司马懿 1 61 5 诸葛亮 2 42 ...
- abp9 .net8 升级错误记录
错误一. Cannot find compilation library location for package 'System.Security.Cryptography.Pkcs' 修复方法: ...