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 ...
随机推荐
- 内存缓存 Gcache VS Caffeine源码详解
转一篇.后续再尝试自己实践一下
- 酷呆桌面 CooDesker 桌面整理工具 - 软件推荐
酷呆桌面 CooDesker 桌面整理工具 - 软件推荐 推荐理由 满足了我对桌面映射到某一目录的需求,这样桌面就真的干净了 免费且没有广告 可进入目录继续延展,双击空白地方返回上一层,非常方便 5M ...
- 【图算法】构建消息传递网络教程 Creating Message Passing Networks by Pytorch-geometric
一.背景 将卷积运算推广到不规则域通常表示为邻局聚合(neighborhood aggregation)或消息传递(neighborhood aggregation)模式. \(\mathbf{x}^ ...
- Java使用Steam流对数组进行排序
原文地址:Java使用Steam流对数组进行排序 - Stars-One的杂货小窝 简单记下笔记,不是啥难的东西 sorted()方法里传了一个比较器的接口 File file = new File( ...
- window.showModalDialog与opener及returnValue
首先来看看 window.showModalDialog 的参数 vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatu ...
- LinuxDNS分析从入门到放弃(记一次有趣的dns问题排查记录,ping 源码分析,getaddrinfo源码分析)
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 环境说明 ubuntu 18.04 前言 我们这里有一块 ...
- MapStructPlus 1.2.5 发布,新增 Solon 支持
MapStructPlus 1.2.5 发布,更新内容如下: fix: 解决 MapConvertMapperAdapter 编译警告问题 feat: 增加 nullValueMappingStrat ...
- vue项目本地开发完成后部署到服务器后报404是什么原因呢?
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 一.如何部署 前后端分离开发模式下,前后端是独立布署的,前端只需要将最后的构建物上传至目标服务器的web容器指定的静态目录下即可 我们知道 ...
- verilog之function
verilog之function 1.基本作用 function,就是声明一个函数.与task的区别就是有参数.function的返回值就是函数名(可以设置位宽),输入值任意,均作为输入参数.代码块需 ...
- 英语文档阅读学习系列之ZYNQ-7000 All Programmable SOC Packaging and Pinout
UG865-Zynq-7000-pkg-pinout 1.Table 一个overview和其他部分的构成一个整体. 2.overview This section describes the pin ...