前端vue单个文件上传支持图片,压缩包以及文件 , 下载完整代码请访问uni-app插件市场址:https://ext.dcloud.net.cn/plugin?id=13066
前端vue单个文件上传支持图片,压缩包以及文件 , 下载完整代码请访问uni-app插件市场址:https://ext.dcloud.net.cn/plugin?id=13066
效果图如下:
使用方法
<!-- photoList:选择的图片数组 @click:图片选择事件-->
<ccChooseImgsView :photoList="photoList" @click="addPhotoClick"></ccChooseImgsView>
addPhotoClick() {
uni.hideLoading();
let myThis = this;
if (myThis.photoList.length >= 1) {
myThis.photoList = [];
}
uni.chooseImage({
count: 1,
sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: function(res) {
myThis.photoList = [{'filePath':res.tempFilePaths[0]}];
console.log('选择图片 =' + JSON.stringify(myThis.photoList));
}
});
},
if (this.photoList.length > 0) {
myFilePath = this.photoList[0].filePath;
}
// 服务器地址上传地址 仅为示例,非真实的接口地址
let baseUrl = "http://gzcc.com/cc//appSc/up"
uni.uploadFile({
url: baseUrl, //仅为示例,非真实的接口地址
filePath: myFilePath, //文件路径
name: 'image', //服务端文件接受key
formData: this.myParamData,
success: (uploadFileRes) => {
uni.hideLoading();
let dataDic = JSON.parse(uploadFileRes.data);
console.log('uploadFileRes成功 = ' + JSON.stringify(uploadFileRes));
console.log('datadic成功 = ' + typeof(dataDic));
console.log('code码 = ' + dataDic['code']);
}
});
HTML代码部分
<template>
<view class="content" v-if="seen">
<form @submit="formSubmit" @reset="formReset">
<view class="inputView">
<text class="leftTitle">交通指引</text>
</view>
<textarea class="rightTextarea" name="direct" placeholder=" 请输入交通指引" />
<view class="inputView">
<text class="leftTitle">房屋介绍</text>
</view>
<textarea class="rightTextarea" name="village" placeholder=" 请输入房屋介绍" />
<view class="inputView">
<text class="leftTitle">添加房源照片(仅可添加1张)</text>
</view>
<!-- photoList:选择的图片数组 @click:图片选择事件-->
<ccChooseImgsView :photoList="photoList" @click="addPhotoClick"></ccChooseImgsView>
<view class="uni-btn-v">
<button class="botBtn" type="primary" form-type="submit">提交</button>
<view class="tipText"> 注意事项: 请确保您填写的房屋信息真实无误 </view>
</view>
</form>
</view>
</template>
JS代码 (引入组件 填充数据)
<script>
import Vue from 'vue';
import ccChooseImgsView from '../../components/ccChooseImgsView.vue';
export default {
components: {
ccChooseImgsView
},
data() {
return {
photoList: [],
seen: true,
myParamData: {},
isClick: false,
};
},
onLoad: function(e) {
},
methods: {
formSubmit: function(e) {
console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value));
if (this.isClick) {
let that = this;
setTimeout(function() {
that.isClick = false;
}, 600)
return;
}
this.isClick = true;
var formdata = e.detail.value;
this.myParamData = Object.assign(this.myParamData, formdata);
console.log('页面3 myParamData=' + JSON.stringify(this.myParamData));
if (formdata['direct'].length < 2) {
uni.showModal({
content: '请输入交通指引',
showCancel: false
});
return;
}
if (formdata['village'].length < 2) {
uni.showModal({
content: '请输入所在小区介绍',
showCancel: false
});
return;
}
if (this.photoList.length < 1) {
uni.showModal({
content: '请添加房源照片',
showCancel: false
});
return;
}
uni.showLoading({
title: '上传中'
})
let myFilePath = '';
if (this.photoList.length > 0) {
myFilePath = this.photoList[0].filePath;
}
// 服务器地址上传地址 仅为示例,非真实的接口地址
let baseUrl = "http://gzcc.com/cc//appSc/up"
uni.uploadFile({
url: baseUrl, //仅为示例,非真实的接口地址
filePath: myFilePath, //文件路径
name: 'image', //服务端文件接受key
formData: this.myParamData,
success: (uploadFileRes) => {
uni.hideLoading();
let dataDic = JSON.parse(uploadFileRes.data);
console.log('uploadFileRes成功 = ' + JSON.stringify(uploadFileRes));
console.log('datadic成功 = ' + typeof(dataDic));
console.log('code码 = ' + dataDic['code']);
}
});
},
addPhotoClick() {
uni.hideLoading();
let myThis = this;
if (myThis.photoList.length >= 1) {
myThis.photoList = [];
}
uni.chooseImage({
count: 1,
sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: function(res) {
myThis.photoList = [{'filePath':res.tempFilePaths[0]}];
console.log('选择图片 =' + JSON.stringify(myThis.photoList));
}
});
},
}
};
</script>
CSS
<style>
.uni-form-item .title {
padding: 20rpx 0;
}
.content {
display: flex;
flex-direction: column;
width: 100%;
height: auto;
}
.inputView {
flex-direction: row;
display: flex;
height: 40px;
align-items: center;
width: 100%;
}
.line {
width: 90%;
height: 2rpx;
margin-left: -2rpx;
background-color: #f8f8f8;
margin-left: 5%;
}
.leftTitle {
margin-left: 40rpx;
width: 284px;
height: 32px;
line-height: 32px;
font-size: 28rpx;
color: #333333;
}
.rightTextarea {
margin-left: 5%;
width: 90%;
height: 106px;
line-height: 40rpx;
border-radius: 12rpx;
border: solid 1px #F5F5F5;
font-size: 15px;
}
.uni-btn-v {
width: 100%;
height: auto;
}
.botBtn {
width: 90%;
margin-top: 36px;
height: 48px;
}
.tipText {
width: 100%;
margin-left: 0px;
text-align: center;
color: #666666;
margin-top: 36px;
margin-bottom: 36px;
font-size: 28rpx;
}
</style>
前端vue单个文件上传支持图片,压缩包以及文件 , 下载完整代码请访问uni-app插件市场址:https://ext.dcloud.net.cn/plugin?id=13066的更多相关文章
- thinkphp文件上传以及图片处理
文件上传 上传表单 在ThinkPHP中使用上传功能无需进行特别处理.例如,下面是一个带有附件上传的表单提交: <form action="__URL__/upload" e ...
- vue中el-upload上传多图片且携带参数,批量而不是一张一张的解决方案
现在前端基本不是vue技术栈就是react技术栈. vue技术栈最常用的就是element-ui的ui框架了. 在项目中,我们经常会碰到这种需求:批量上传文件 element-ui 确实也为我们提供了 ...
- MVC&WebForm对照学习:文件上传(以图片为例)
原文 http://www.tuicool.com/articles/myM7fe 主题 HTMLMVC模式Asp.net 博客园::首页:: :: :: ::管理 5 Posts :: 0 ...
- mvc文件上传支持批量上传,拖拽以及预览,文件内容校验等
使用bootstrap-fileinput 使用方式: 1.nuget:Install-Package bootstrap-fileinput 2.语言本地化{下载fileinput_locale_z ...
- .net core版 文件上传/ 支持批量上传,拖拽以及预览,bootstrap fileinput上传文件
asp.net mvc请移步 mvc文件上传支持批量上传,拖拽以及预览,文件内容校验 本篇内容主要解决.net core中文件上传的问题 开发环境:ubuntu+vscode 1.导入所需要的包:n ...
- skymvc文件上传支持多文件上传
skymvc文件上传支持多文件上传 支持单文件.多文件上传 可以设定 文件大小.存储目录.文件类型 //上传的文件目录 $this->upload->uploaddir="att ...
- struts文件上传,获取文件名和文件类型
struts文件上传,获取文件名和文件类型 Action中还有两个属 性:uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名.文件类型.这是S ...
- 【FTP】java FTPClient 文件上传内容为空,文件大小为0
问题:如题所述,使用FTPClient上传至FTP服务器, 表现如下:①文件大小为0 ②上传很小的文件,但是要花费很长的时间,20K要花费2分钟甚至更久 ③没有任何的报错,没有任何的乱码 解决方法: ...
- Spring4 MVC 多文件上传(图片并展示)
开始需要在pom.xml加入几个jar,分别是 <dependency> <groupId>commons-fileupload</groupId> <art ...
- spring实现文件上传(图片解析)
合抱之木,生于毫末,千里之行,始于足下,要想了解spring的文件上传功能,首先要知道spring是通过流的方式将文件进行解析,然后上传.那么是不是所有需要用的文件上传的地方都要写一遍文件解析器呢? ...
随机推荐
- 任意Exe转ShellCode?
之前写过一个远控,但一直在琢磨如何生成shellcode,今天偶然看见一个项目:sRDI,github上就有 这个项目主要就是将dll转成shellcode,于是我就想到了"写一个输出文件的 ...
- 聊一聊如何使用Crank给我们的类库做基准测试
目录 背景 什么是 Crank 入门示例 Pull Request 总结 参考资料 背景 当我们写了一个类库提供给别人使用时,我们可能会对它做一些基准测试来测试一下它的性能指标,好比内存分配等. 在 ...
- Java设计模式 —— 享元模式
14 享元模式 14.1 享元模式概述 Flyweight Pattern: 运用共享技术有效地支持大量细粒度对象的复用. 当系统中存在大量相同或相似的对象时,它通过共享技术实现相同或相似的细粒度对象 ...
- Java GC基础知识
对象存活判断 引用计数 在对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加一:当引用失效时,计数器值就减一:任何时刻计数器为零的对象就是不可 能再被使用的 引用计数法的缺陷: publi ...
- pytorch在有限的资源下部署大语言模型(以ChatGLM-6B为例)
pytorch在有限的资源下部署大语言模型(以ChatGLM-6B为例) Part1知识准备 在PyTorch中加载预训练的模型时,通常的工作流程是这样的: my_model = ModelClass ...
- javasec(二)class文件结构
这篇文章介绍java的class文件结构. 深入理解Java虚拟机(类文件结构) 我们所编写的每一行代码,要在机器上运行最终都需要编译成二进制的机器码 CPU 才能识别.但是由于虚拟机的存在,屏蔽了操 ...
- asyncio的基本使用框架,python高效处理数据,asyncio.gather(),asyncio. create_task(),asyncio.run(main())
asyncio 是 Python 3.4 引入的标准库,是一个基于事件循环的异步 I/O 并发库.它提供了一种协作式的多任务处理方式,使得我们能够在一个线程中并发处理多个 I/O 操作.它通过将 I/ ...
- TensorFlow - 框架实现中的三种 Graph
文章目录 TensorFlow - 框架实现中的三种 Graph 1. Graph 2. GraphDef 3. MetaGraph 4. Checkpoint 5. 总结 TensorFlow - ...
- [C++基础入门] 7、 指针
文章目录 7 指针 7.1 指针的基本概念 7.2 指针变量的定义和使用 7.3 指针所占内存空间 7.4 空指针和野指针 7.5 const修饰指针 7.6 指针和数组 7.7 指针和函数 7.8 ...
- 使用Jmeter进行CPU、内存等监控
一.需要的准备 1.jp@gc - PerfMon Metrics Collector插件(安装方法就不过多介绍啦!) 2.ServerAgent服务器(下载:https://github.com/u ...