前端vue单个文件上传支持图片,压缩包以及文件

随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。通过组件化开发,可以有效实现单独开发,单独维护,而且他们之间可以随意的进行组合。大大提升开发效率低,降低维护成本。

组件化对于任何一个业务场景复杂的前端应用以及经过多次迭代之后的产品来说都是必经之路。组件化要做的不仅仅是表面上看到的模块拆分解耦,其背后还有很多工作来支撑组件化的进行,例如结合业务特性的模块拆分策略、模块间的交互方式和构建系统等等 。

本文给大家介绍的一款组件是:

前端vue单个文件上传支持图片,压缩包以及文件 ,

阅读全文下载完整组件代码请关注微信公众号: 前端组件开发

 

d848d5658a07453c843277846948c608.png

效果图如下:

 

5890a51c8ecb2d2493c4395a3d3e523d.png

480b7d714febbd63d5c32714c432ae50.png

#### 使用方法

```使用方法

<!-- 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代码部分

```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代码 (引入组件 填充数据)

```javascript

<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

```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>

```

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端组件开发

你的钟意将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值