Commit a45585d8 by kyle

上传pdf

parent fc3b85f1
<template>
<view class="" :style="{opacity: opacity}">
<lsj-upload ref="lsjUpload" childId="upload1" :width="width" :height="height" :option="option" :size="size"
:formats="formats" :debug="debug" :instantly="instantly" @progress="onProgress" @change="onChange" @uploadEnd="onuploadEnd" :opacity="opacity">
<!-- <view class="btn" :style="{width: width,height: height}">选择附件</view> -->
</lsj-upload>
<!-- <view class="padding"> -->
<!-- <view>已选择文件列表:</view> -->
<!-- #ifndef MP-WEIXIN -->
<!-- <view v-for="(item,index) in files.values()" :key="index">
<image style="width: 100rpx;height: 100rpx;" :src="item.path" mode="widthFix"></image>
<text>{{item.path}}</text>
<text>{{item.name}}</text>
<text style="margin-left: 10rpx;">大小:{{item.size}}</text>
<text style="margin-left: 10rpx;">状态:{{item.type}}</text>
<text style="margin-left: 10rpx;">进度:{{item.progress}}</text>
<text style="margin-left: 10rpx;" v-if="item.responseText">服务端返回演示:{{item.responseText.code}}</text>
<text @click="clear(item.name)"
style="margin-left: 10rpx;padding: 0 10rpx;border: 1rpx solid #007AFF;">删除</text>
</view> -->
<!-- #endif -->
<!-- #ifdef MP-WEIXIN -->
<!-- <view v-for="(item,index) in wxFiles" :key="index">
<text>{{item.name}}</text>
<text style="margin-left: 10rpx;">大小:{{item.size}}</text>
<text style="margin-left: 10rpx;">状态:{{item.type}}</text>
<text style="margin-left: 10rpx;">进度:{{item.progress}}</text>
<view>
<button>删除</button>
</view>
</view> -->
<!-- #endif -->
<!-- </view> -->
</view>
</template>
<script>
import {
apiURL
} from "@/environments/environment.ts";
export default {
name: "uploadFile",
props: ['requestVO','deformats','opacity','instantly'],
emits:['sendPath'],
data() {
return {
// 上传接口参数
option: {
// 上传服务器地址,此地址需要替换为你的接口地址
url: `${apiURL}/file/upload`,
// 上传附件的key
name: 'file',
// 根据你接口需求自定义请求头
header: {
'X-Authorization': uni.getStorageSync('uni-token') ? uni.getStorageSync('uni-token') : ''
},
// 根据你接口需求自定义body参数
formData: {
requestVO: JSON.parse(this.requestVO)
}
},
// 选择文件后是否立即自动上传,true=选择后立即上传
instantly: true,
// 必传宽高且宽高应与slot宽高保持一致
width: '100%',
height: '100rpx',
// 限制允许选择的格式,空串=不限制,默认为空png,jpg,mp4,pdf,.docx
formats: this.deformats,
// 文件上传大小限制
size: 50,
// 文件回显列表
files: new Map(),
// 微信小程序Map对象for循环不显示,所以转成普通数组,不要问为什么,我也不知道
wxFiles: [],
// 是否打印日志
debug: true,
// 演示用
tabIndex: 0,
list: [],
}
},
onReady() {
setTimeout(() => {
console.log('----演示动态更新参数-----');
this.$refs.lsjUpload.setData('formData.orderId', '动态设置的参数');
console.log('以下注释内容为-动态更新参数更多演示,放开后可查看演示效果');
// 修改option对象的name属性
// this.$refs.lsjUpload.setData('name','myFile');
// 修改option对象的formData内的属性
// this.$refs.lsjUpload.setData('formData.appid','1111');
// 替换option对象的formData
// this.$refs.lsjUpload.setData('formData',{appid:'222'});
// option对象的formData新增属性
// this.$refs.lsjUpload.setData('formData.newkey','新插入到formData的属性');
// ---------演示初始化值,用于已提交后再次编辑时需带入已上传文件-------
// 方式1=传入数组
let files1 = [{
name: '1.png'
},
{
name: '2.png',
}
];
// 方式2=传入Map对象
let files2 = new Map();
files2.set('1.png', {
name: '1.png'
})
// 设置初始files列表
this.$refs.lsjUpload.setFiles(files1);
}, 2000)
},
methods: {
// 某文件上传结束回调(成功失败都回调)
onuploadEnd(item) {
console.log(`${item.name}已上传结束,上传状态=${item.type}`);
// 更新当前状态变化的文件
// this.files.set(item.name, item);
// 演示上传完成后取服务端数据
if (item['responseText']) {
console.log('演示服务器返回的字符串JSON转对象');
// this.files.get(item.name).responseText = JSON.parse(item.responseText);
this.$emit('sendPath',item.responseText)
}
// 微信小程序Map对象for循环不显示,所以转成普通数组,不要问为什么,我也不知道
// #ifdef MP-WEIXIN
this.wxFiles = [...this.files.values()];
// #endif
// 强制更新视图
this.$forceUpdate();
// ---可删除--演示判断是否所有文件均已上传成功
let isAll = [...this.files.values()].find(item => item.type !== 'success');
if (!isAll) {
console.log('已全部上传完毕');
} else {
console.log(isAll.name + '待上传');
}
},
// 上传进度回调
onProgress(item) {
// 更新当前状态变化的文件
// this.files.set(item.name, item);
// console.log('打印对象', JSON.stringify(this.files.get(item.name)));
// 微信小程序Map对象for循环不显示,所以转成普通数组,不要问为什么,我也不知道
// #ifdef MP-WEIXIN
this.wxFiles = [...this.files.values()];
// #endif
// 强制更新视图
this.$forceUpdate();
},
// 文件选择回调
onChange(files) {
// 更新选择的文件
this.files = files;
// 强制更新视图
this.$forceUpdate();
// 微信小程序Map对象for循环不显示,所以转成普通数组,不要问为什么,我也不知道
// #ifdef MP-WEIXIN
this.wxFiles = [...this.files.values()];
// #endif
},
// 手动上传
upload() {
// name=指定文件名,不指定则上传所有type等于waiting和fail的文件
this.$refs.lsjUpload.upload();
},
// 移除某个文件
clear(name) {
// name=指定文件名,不传name默认移除所有文件
this.$refs.lsjUpload.clear(name);
},
/**
* 以下为演示
*/
// DOM重排演示,重排后组件内部updated默认会触发show方法,若特殊情况未能触发updated也可以手动调用一次show()
// 什么是DOM重排?自行百度去~
add() {
this.list.push('DOM重排测试');
},
// 切换视图演示,APP端因为是webview,层级比view高,
// 此时若不希望点击触发选择文件,需要手动调用hide()
// 手动调用hide后,需要调用show()才能恢复触发面
onTab(tabIndex) {
this.tabIndex = tabIndex;
if (tabIndex == 0) {
this.$nextTick(() => {
this.$refs.lsjUpload.show();
})
} else {
this.$refs.lsjUpload.hide();
}
},
// 打开nvue窗口
open() {
uni.navigateTo({
url: '/pages/nvue-demo/nvue-demo'
});
}
}
}
</script>
<style>
</style>
......@@ -41,6 +41,9 @@
</view>
<view class="uploadpdf" @click="uploadFile()">
<view class="uploadimg">
<view class="uploadFileBox" v-if="status != 3">
<uploadFile :deformats="formats" :opacity="0" ref="uploadRef" :height="'320rpx'" :instantly="'true'" :requestVO="JSON.stringify(dataForm)" @sendPath="getPath"></uploadFile>
</view>
<view class="" style="width: 50px;">
<image class="image" src="../../static/myteam/Group1646.png"></image>
</view>
......@@ -61,13 +64,14 @@
</template>
<script>
import {
uploadFilepdf
} from '@/util/uploaderFile'
import {ref} from "vue";
import uploadFile from "@/components/uploadFile/uploadFile.vue";
import api from "@/api/api";
export default {
components:{uploadFile},
data() {
return {
formats:'pdf',
dataForm: {
loginId: "1",
targetType: "5",
......@@ -100,6 +104,9 @@
},
methods: {
getPath(e){
console.log(e,'=======================')
},
async getquerySignUpInfo() {
let UserSignUpInfoQueryRequestVO = {
userSignUpId:this.openForm.signupId
......@@ -162,19 +169,9 @@
icon: 'none'
});
return false
}else{
}
let that = this;
uploadFilepdf(this.dataForm)
.then(res => {
console.log(res, 54854)
// that.headUrl = res[0].url
// this.fileForm = {
// name: res.name,
// filePath: res.data.filePath
// }
this.openForm.fileName = res.name
this.openForm.planBookPdfUrl = res.data.filePath
})
},
}
}
......@@ -202,12 +199,19 @@
.uploadimg {
height: 160px;
position: relative;
height: 320rpx;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
.uploadFileBox{
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
.image {
width: 50px;
height: 43px;
......
## 2.2.3(2022-12-06)
修复手动调用show()导致count失效的问题
## 2.2.2(2022-12-01)
Vue3自行修改兼容
## 2.2.1(2022-10-19)
修复childId警告提示
## 2.2.0(2022-10-10)
更新app端webview窗口参数clidId,默认值添加时间戳保证唯一性
## 2.1.9(2022-07-13)
[修复] app端选择文件后初始化设置的文件列表被清空问题
## 2.1.8(2022-07-13)
[新增] ref方法初始化文件列表,用于已提交后再次编辑时需带入已上传文件:setFiles(files),可传入数组或Map对象,传入格式请与组件选择返回格式保持一致,且name为必须属性。
## 2.1.7(2022-07-12)
修复ios端偶现创建webview初始化参数未生效的问题
## 2.1.6(2022-07-11)
[修复]:修复上个版本更新导致nvue窗口组件不能选择文件的问题;
[新增]
1.应群友建议(填写禁止格式太多)格式限制formats由原来填写禁止选择的格式改为填写允许被选择的格式;
2.应群友建议(增加上传结束回调事件),上传结束回调事件@uploadEnd
3.如能帮到你请留下你的免费好评,组件使用过程中有问题可以加QQ群交流,至于Map对象怎么使用这类前端基础问题请自行百度
## 2.1.5(2022-07-01)
app端组件销毁时添加自动销毁webview功能,避免v-if销毁组件的情况控件还能被点击的问题
## 2.1.4(2022-07-01)
修复小程序端回显问题
## 2.1.3(2022-06-30)
回调事件返回参数新增path字段(文件临时地址),用于回显
## 2.1.2(2022-06-16)
修复APP端Tabbar窗口无法选择文件的问题
## 2.1.1(2022-06-16)
优化:
1.组件优化为允许在v-if中使用;
2.允许option直接在data赋值,不再强制在onRead中初始化;
## 2.1.0(2022-06-13)
h5 pc端更改为单次可多选
## 2.0.9(2022-06-10)
更新演示内容,部分同学不知道怎么获取服务端返回的数据
## 2.0.8(2022-06-09)
优化动态更新上传参数函数,具体查看下方说明:动态更新参数演示
## 2.0.7(2022-06-07)
新增wxFileType属性,用于小程序端选择附件时可选文件类型
## 2.0.6(2022-06-07)
修复小程序端真机选择文件提示失败的问题
## 2.0.5(2022-06-02)
优化小程序端调用hide()后未阻止触发文件选择问题
## 2.0.4(2022-06-01)
优化APP端选择器初始定位
## 2.0.3(2022-05-31)
修复nvue窗口选择文件报错问题
## 2.0.2(2022-05-20)
修复ios端opiton设置过早未传入webview导致不自动上传问题
## 2.0.1(2022-05-19)
修复APP端子窗口点击选择文件不响应问题
## 2.0.0(2022-05-18)
此次组件更新至2.0版本,与1.0版本使用上略有差异,已使用1.0的同学请自行斟酌是否需要升级!
部分差异:
一、 2.0新增异步触发上传功能;
二、2.0新增文件批量上传功能;
三、2.0优化option,剔除属性,只保留上传接口所需字段,且允许异步更改option的值;
四、组件增加size(文件大小限制)、count(文件个数限制)、formats(文件后缀限制)、accept(文件类型限制)、instantly(是否立即自动上传)、debug(日志打印)等属性;
五、回调事件取消input事件、callback事件,新增change事件和progress事件;
六、ref事件新增upload事件、clear事件;
七、优化组件代码,show和hide函数改为显示隐藏,不再重复开关webview;
## 1.2.3(2022-03-22)
修复Demo里传入待完善功能[手动上传属性manual=true]导致不自动上传的问题,手动提交上传待下个版本更新
## 1.2.2(2022-02-21)
修复上版本APP优化导致H5和小程序端不自动初始化的问题,此次更新仅修复此问题。异步提交功能下个版本更新~
## 1.2.1(2022-01-25)
QQ1群已满,已开放2群:469580165
## 1.2.0(2021-12-09)
优化APP端页面中DOM重排后每次需要重新定位的问题
## 1.1.1(2021-12-09)
优化,与上版本使用方式有改变,请检查后确认是否需要更新,create更名为show, close更名为hide,取消初始化时手动create, 传参方式改为props=>option
## 1.1.0(2021-12-09)
新增refresh方法,用于DOM发生重排时重新定位控件(APP端)
## 1.0.9(2021-07-15)
修复上传进度未同步渲染,直接返回100%的BUG
## 1.0.8(2021-07-12)
修复H5端传入height和width未生效的bug
## 1.0.7(2021-07-07)
修复h5和小程序端上传完成callback未返回fileName字段问题
## 1.0.6(2021-07-07)
修复h5端提示信息debug
## 1.0.5(2021-06-29)
感谢小伙伴找出bug,上传成功回调success未置为true,已修复
## 1.0.4(2021-06-28)
新增兼容APP,H5,小程序手动关闭控件,关闭后不再弹出文件选择框,需要重新create再次开启
## 1.0.3(2021-06-28)
close增加条件编译,除app端外不需要close
## 1.0.2(2021-06-28)
1.修复页面滚动位置后再create控件导致控件位置不正确的问题;
2.修复nvue无法create控件;
3.示例项目新增nvue使用案例;
## 1.0.1(2021-06-28)
因为有的朋友不清楚app端切换tab时应该怎么处理webview,现重新上传一版示例项目,需要做tab切换的朋友可以导入示例项目查看
## 1.0.0(2021-06-25)
此插件为l-file插件中上传功能改版,更新内容为:
1. 按钮内嵌入页面,不再强制固定底部,可跟随页面滚动
2.无需再单独弹框点击上传,减去中间层
3.通过slot自定义按钮样式
<template>
<view class="lsj-file" :style="[getStyles]">
<view ref="lsj" class="hFile" :style="[getStyles]" @click="onClick">
<slot><view class="defview" :style="[getStyles]">附件上传</view></slot>
</view>
</view>
</template>
<script>
// 查看文档:https://ext.dcloud.net.cn/plugin?id=5459
import {LsjFile} from './LsjFile.js'
export default {
name: 'Lsj-upload',
props: {
// 打印日志
debug: {type: Boolean,default: false},
// 自动上传
instantly: {type: Boolean,default: false},
// 上传接口参数设置
option: {type: Object,default: ()=>{}},
// 文件大小上限
size: { type: Number, default: 10 },
// 文件选择个数上限,超出后不触发点击
count: { type: Number, default: 9 },
// 允许上传的文件格式(多个以逗号隔开)
formats: { type: String, default:''},
// input file选择限制
accept: {type: String,default: ''},
// 微信选择文件类型
//all=从所有文件选择,
//video=只能选择视频文件,
//image=只能选择图片文件,
//file=可以选择除了图片和视频之外的其它的文件
wxFileType: { type: String, default: 'all' },
// webviewID需唯一,不同窗口也不要同Id
childId: { type: String, default: 'lsjUpload' },
// 文件选择触发面宽度
width: { type: String, default: '100%' },
// 文件选择触发面高度
height: { type: String, default: '80rpx' },
// top,left,bottom,right仅position=absolute时才需要传入
top: { type: [String, Number], default: '' },
left: { type: [String, Number], default: '' },
bottom: { type: [String, Number], default: '' },
right: { type: [String, Number], default: '' },
// nvue不支持跟随窗口滚动
position: {
type: String,
// #ifdef APP-NVUE
default: 'absolute',
// #endif
// #ifndef APP-NVUE
default: 'static',
// #endif
},
},
data() {
return {
}
},
watch: {
option(v) {
// #ifdef APP-PLUS
this.lsjFile&&this.show();
// #endif
}
},
updated() {
// #ifdef APP-PLUS
if (this.isShow) {
this.lsjFile&&this.show();
}
// #endif
},
computed: {
getStyles() {
let styles = {
width: this.width,
height: this.height
}
if (this.position == 'absolute') {
styles['top'] = this.top
styles['bottom'] = this.bottom
styles['left'] = this.left
styles['right'] = this.right
styles['position'] = 'fixed'
}
return styles
}
},
mounted() {
this._size = 0;
let WEBID = this.childId + new Date().getTime();
this.lsjFile = new LsjFile({
id: WEBID,
debug: this.debug,
width: this.width,
height: this.height,
option: this.option,
instantly: this.instantly,
// 限制条件
prohibited: {
// 大小
size: this.size,
// 允许上传的格式
formats: this.formats,
// 限制选择的格式
accept: this.accept,
count: this.count
},
onchange: this.onchange,
onprogress: this.onprogress,
});
this.create();
},
beforeDestroy() {
// #ifdef APP-PLUS
this.lsjFile.dom.close();
// #endif
},
methods: {
setFiles(array) {
if (array instanceof Map) {
for (let [key, item] of array) {
item['progress'] = 100;
item['type'] = 'success';
this.lsjFile.files.set(key,item);
}
}
else if (Array.isArray(array)) {
array.forEach(item=>{
if (item.name) {
item['progress'] = 100;
item['type'] = 'success';
this.lsjFile.files.set(item.name,item);
}
});
}
this.onchange(this.lsjFile.files);
},
setData() {
this.lsjFile&&this.lsjFile.setData(...arguments);
},
getDomStyles(callback) {
// #ifndef APP-NVUE
let view = uni
.createSelectorQuery()
.in(this)
.select('.lsj-file')
view.fields(
{
size: true,
rect: true
},
({ height, width, top, left, right, bottom }) => {
uni.createSelectorQuery()
.selectViewport()
.scrollOffset(({ scrollTop }) => {
return callback({
top: parseInt(top) + parseInt(scrollTop) + 'px',
left: parseInt(left) + 'px',
width: parseInt(width) + 'px',
height: parseInt(height) + 'px'
})
})
.exec()
}
).exec()
// #endif
// #ifdef APP-NVUE
const dom = weex.requireModule('dom')
dom.getComponentRect(this.$refs.lsj, ({ size: { height, width, top, left, right, bottom } }) => {
return callback({
top: parseInt(top) + 'px',
left: parseInt(left) + 'px',
width: parseInt(width) + 'px',
height: parseInt(height) + 'px',
right: parseInt(right) + 'px',
bottom: parseInt(bottom) + 'px'
})
})
// #endif
},
show() {
if (this._size && (this._size >= this.count)) {
return;
}
this.isShow = true;
// #ifdef APP-PLUS
this.lsjFile&&this.getDomStyles(styles => {
this.lsjFile.dom.setStyle(styles)
});
// #endif
// #ifdef H5
this.lsjFile.dom.style.display = 'inline'
// #endif
},
hide() {
this.isShow = false;
// #ifdef APP-PLUS
this.lsjFile&&this.lsjFile.dom.setStyle({
top: '-100px',
left:'0px',
width: '1px',
height: '100px',
});
// #endif
// #ifdef H5
this.lsjFile.dom.style.display = 'none'
// #endif
},
/**
* 手动提交上传
* @param {string}name 文件名称,不传则上传所有type等于waiting和fail的文件
*/
upload(name) {
console.log(name,'===================================');
this.lsjFile&&this.lsjFile.upload(name);
},
/**
* @returns {Map} 已选择的文件Map集
*/
onchange(files) {
this.$emit('change',files);
this._size = files.size;
return files.size >= this.count ? this.hide() : this.show();
},
/**
* @returns {object} 当前上传中的对象
*/
onprogress(item,end=false) {
this.$emit('progress',item);
if (end) {
setTimeout(()=>{
this.$emit('uploadEnd',item);
},0);
}
},
/**
* 移除组件内缓存的某条数据
* @param {string}name 文件名称,不指定默认清除所有文件
*/
clear(name) {
this.lsjFile.clear(name);
},
// 创建选择器
create() {
// 若iOS端服务端处理不了跨域就将hybrid目录内的html放到服务端去,并将此处path改成服务器上的地址
let path = '/uni_modules/lsj-upload/hybrid/html/uploadFile.html';
let dom = this.lsjFile.create(path);
// #ifdef H5
this.$refs.lsj.$el.appendChild(dom);
// #endif
// #ifndef APP-PLUS
this.show();
// #endif
// #ifdef APP-PLUS
dom.setStyle({position: this.position});
dom.loadURL(path);
setTimeout(()=>{
// #ifdef APP-NVUE
plus.webview.currentWebview().append(dom);
// #endif
// #ifndef APP-NVUE
this.$root.$scope.$getAppWebview().append(dom);
// #endif
this.show();
},300)
// #endif
},
// 点击选择附件
onClick() {
if (this._size >= this.count) {
this.toast(`只允许上传${this.count}个文件`);
return;
}
// #ifdef MP-WEIXIN
if (!this.isShow) {return;}
let count = this.count - this._size;
this.lsjFile.chooseMessageFile(this.wxFileType,count);
// #endif
},
toast(msg) {
uni.showToast({
title: msg,
icon: 'none'
});
}
}
}
</script>
<style scoped>
.lsj-file {
display: inline-block;
}
.defview {
/* background-color: #007aff; */
color: #fff;
/* border-radius: 10rpx; */
display: flex;
align-items: center;
justify-content: center;
font-size: 28rpx;
}
.hFile {
position: relative;
overflow: hidden;
}
</style>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title class="title">[文件管理器]</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<style type="text/css">
.content {background: transparent;}
.btn {position: relative;top: 0;left: 0;bottom: 0;right: 0;}
.btn .file {position: fixed;z-index: 93;left: 0;right: 0;top: 0;bottom: 0;width: 100%;opacity: 0;}
</style>
</head>
<body>
<div id="content" class="content">
<div class="btn">
<input multiple @change="onChange" :accept="accept" ref="file" class="file" type="file" />
</div>
</div>
<script type="text/javascript" src="js/vue.min.js"></script>
<script type="text/javascript">
let _this;
var vm = new Vue({
el: '#content',
data: {
accept: '',
},
mounted() {
console.log('加载webview');
_this = this;
this.files = new Map();
document.addEventListener('plusready', (e)=>{
let {debug,instantly,prohibited} = plus.webview.currentWebview();
this.debug = debug;
this.instantly = instantly;
this.prohibited = prohibited;
this.accept = prohibited.accept;
location.href = 'callback?retype=updateOption';
}, false);
},
methods: {
toast(msg) {
plus.nativeUI.toast(msg);
},
clear(name) {
if (!name) {
this.files.clear();
return;
}
this.files.delete(name);
},
setData(option='{}') {
debugger;
this.debug&&console.log('更新参数:'+option);
try{
_this.option = JSON.parse(option);
}catch(e){
console.error('参数设置错误');
console.error(e);
}
},
async upload(name=''){
if (name && this.files.has(name)) {
await this.createUpload(this.files.get(name));
}
else {
for (let item of this.files.values()) {
if (item.type === 'waiting' || item.type === 'fail') {
await this.createUpload(item);
}
}
}
},
onChange(e) {
let fileDom = this.$refs.file;
let file = fileDom.files[0];
let name = file.name;
fileDom.value = '';
this.debug&&console.log('文件名称',name,'大小',file.size);
if (file) {
// 限制文件格式
let suffix = name.substring(name.lastIndexOf(".")+1).toLowerCase();
let formats = this.prohibited.formats.toLowerCase();
if (formats&&!formats.includes(suffix)) {
this.toast(`不支持上传${suffix.toUpperCase()}格式文件`);
return;
}
console.log('this.size',this.prohibited.size);
// 限制文件大小
if (file.size > 1024 * 1024 * Math.abs(this.prohibited.size)) {
this.toast(`附件大小请勿超过${this.prohibited.size}M`)
return;
}
let path = URL.createObjectURL(file);
this.files.set(file.name,{file,path,name: file.name,size: file.size,progress: 0,type: 'waiting'});
this.callChange();
this.instantly&&this.upload();
}
},
/**
* @returns {Map} 已选择的文件Map集
*/
callChange() {
location.href = 'callback?retype=change&files=' + escape(JSON.stringify([...this.files]));
},
/**
* @returns {object} 正在处理的当前对象
*/
changeFilesItem(item,end='') {
this.files.set(item.name,item);
location.href = 'callback?retype=progress&end='+ end +'&item=' + escape(JSON.stringify(item));
},
createUpload(item) {
this.debug&&console.log('准备上传,option=:'+JSON.stringify(this.option));
item.type = 'loading';
delete item.responseText;
return new Promise((resolve,reject)=>{
let {url,name,method='POST',header={},formData={}} = this.option;
let form = new FormData();
for (let keys in formData) {
form.append(keys, JSON.stringify(formData[keys]))
}
form.append(name, item.file);
let xmlRequest = new XMLHttpRequest();
xmlRequest.open(method, url, true);
for (let keys in header) {
xmlRequest.setRequestHeader(keys, header[keys])
}
xmlRequest.upload.addEventListener(
'progress',
event => {
if (event.lengthComputable) {
let progress = Math.ceil((event.loaded * 100) / event.total)
if (progress <= 100) {
item.progress = progress;
this.changeFilesItem(item);
}
}
},
false
);
xmlRequest.ontimeout = () => {
console.error('请求超时')
item.type = 'fail';
this.changeFilesItem(item,true);
return resolve(false);
}
xmlRequest.onreadystatechange = ev => {
if (xmlRequest.readyState == 4) {
if (xmlRequest.status == 200) {
this.debug && console.log('上传完成:' + xmlRequest.responseText)
item['responseText'] = xmlRequest.responseText;
item.type = 'success';
this.changeFilesItem(item,true);
return resolve(true);
} else if (xmlRequest.status == 0) {
console.error('status = 0 :请检查请求头Content-Type与服务端是否匹配,服务端已正确开启跨域,并且nginx未拦截阻止请求')
}
console.error('--ERROR--:status = ' + xmlRequest.status)
item.type = 'fail';
this.changeFilesItem(item,true);
return resolve(false);
}
}
xmlRequest.send(form)
});
}
}
});
</script>
</body>
</html>
{
"id": "lsj-upload",
"displayName": "全文件上传选择非原生2.0版",
"version": "2.2.3",
"description": "文件选择上传-支持APP-H5网页-微信小程序",
"keywords": [
"附件",
"file",
"upload",
"上传",
"文件管理器"
],
"repository": "",
"engines": {
"HBuilderX": "^3.2.16"
},
"dcloudext": {
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "无"
},
"npmurl": "",
"type": "component-vue"
},
"uni_modules": {
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y"
},
"client": {
"App": {
"app-vue": "y",
"app-nvue": "y"
},
"H5-mobile": {
"Safari": "y",
"Android Browser": "y",
"微信浏览器(Android)": "y",
"QQ浏览器(Android)": "y"
},
"H5-pc": {
"Chrome": "y",
"IE": "y",
"Edge": "y",
"Firefox": "y",
"Safari": "y"
},
"小程序": {
"微信": "y",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u"
},
"快应用": {
"华为": "y",
"联盟": "y"
},
"Vue": {
"vue2": "y",
"vue3": "u"
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment