前端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的更多相关文章

  1. thinkphp文件上传以及图片处理

    文件上传 上传表单 在ThinkPHP中使用上传功能无需进行特别处理.例如,下面是一个带有附件上传的表单提交: <form action="__URL__/upload" e ...

  2. vue中el-upload上传多图片且携带参数,批量而不是一张一张的解决方案

    现在前端基本不是vue技术栈就是react技术栈. vue技术栈最常用的就是element-ui的ui框架了. 在项目中,我们经常会碰到这种需求:批量上传文件 element-ui 确实也为我们提供了 ...

  3. MVC&WebForm对照学习:文件上传(以图片为例)

    原文  http://www.tuicool.com/articles/myM7fe 主题 HTMLMVC模式Asp.net 博客园::首页::  ::  ::  ::管理 5 Posts :: 0 ...

  4. mvc文件上传支持批量上传,拖拽以及预览,文件内容校验等

    使用bootstrap-fileinput 使用方式: 1.nuget:Install-Package bootstrap-fileinput 2.语言本地化{下载fileinput_locale_z ...

  5. .net core版 文件上传/ 支持批量上传,拖拽以及预览,bootstrap fileinput上传文件

    asp.net mvc请移步 mvc文件上传支持批量上传,拖拽以及预览,文件内容校验 本篇内容主要解决.net core中文件上传的问题  开发环境:ubuntu+vscode 1.导入所需要的包:n ...

  6. skymvc文件上传支持多文件上传

    skymvc文件上传支持多文件上传 支持单文件.多文件上传 可以设定 文件大小.存储目录.文件类型 //上传的文件目录 $this->upload->uploaddir="att ...

  7. struts文件上传,获取文件名和文件类型

    struts文件上传,获取文件名和文件类型   Action中还有两个属 性:uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名.文件类型.这是S ...

  8. 【FTP】java FTPClient 文件上传内容为空,文件大小为0

    问题:如题所述,使用FTPClient上传至FTP服务器, 表现如下:①文件大小为0 ②上传很小的文件,但是要花费很长的时间,20K要花费2分钟甚至更久 ③没有任何的报错,没有任何的乱码 解决方法: ...

  9. Spring4 MVC 多文件上传(图片并展示)

    开始需要在pom.xml加入几个jar,分别是 <dependency> <groupId>commons-fileupload</groupId> <art ...

  10. spring实现文件上传(图片解析)

    合抱之木,生于毫末,千里之行,始于足下,要想了解spring的文件上传功能,首先要知道spring是通过流的方式将文件进行解析,然后上传.那么是不是所有需要用的文件上传的地方都要写一遍文件解析器呢? ...

随机推荐

  1. Nginx主要功能

    Nginx主要功能: 1.反向代理2.负载均衡3.HTTP服务器(包含动静分离)4.正向代理 一.反向代理 反向代理应该是 Nginx 做的最多的一件事了,什么是反向代理呢,以下是百度百科的说法:反向 ...

  2. Exchangis搭建安装

    项目简介 Exchangis是一个轻量级的.高扩展性的数据交换平台,支持对结构化及无结构化的异构数据源之间的数据传输,在应用层上具有数据权限管控.节点服务高可用和多租户资源隔离等业务特性,而在数据层上 ...

  3. pandas之窗口函数

    为了能更好地处理数值型数据,Pandas 提供了几种窗口函数,比如移动函数(rolling).扩展函数(expanding)和指数加权函数(ewm).窗口函数应用场景非常多.举一个简单的例子:现在有 ...

  4. Redis读书笔记(一)

    Redis数据结构 1 简单动态字符串 Simple dynamic string 的实现 // sds.h/sdshdr struct sdshdr { int len; //记录buf数组中已使用 ...

  5. ubuntu容器的远程xface桌面环境搭建

    一.container: ubuntu20.04 二.commands: apt install xfce4 tigervnc-standalone-server  # xface使用gdm3启动器 ...

  6. EasyExcel配置步骤

    1.介绍 EasyExcel是一个基于Java的简单.省内存的读写Excel的开源项目 参考 https://blog.csdn.net/u013044713/article/details/1202 ...

  7. 龙芯(Loongarch64),在Linux虚拟一个龙芯OS体验下

    前言 想体验下龙芯OS,但是又没有龙芯开发板或者龙芯实体机.手头上只有一个X64环境的Linux发行版,应该怎么做呢? 概括 其实非常简单,可以通过Chroot命令和Qemu在X64的指令集系统上模拟 ...

  8. SpringBoot自定义cron表达式注册定时任务

    springBoot自定义cron表达式注册定时任务 一.原理 1.使用Spring自带的TaskScheduler注册任务 2.注册后返回:ScheduledFuture,用于取消定时任务 3.注册 ...

  9. 15-js语法检查eslint

    const { resolve } = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); modul ...

  10. JVM有意思的图-持续更新

    放一些JVM有意思的图 通过一行代码联想JVM: