uniapp 移动端上传文件

<template>
	<view class="photo-control">
		<text class="photo-header"><text class="red bold ml18" v-if="required">*</text><text class="ml16 light">文件上传</text></text>
		<view class="photo-container">			
			<view  class="photo plus"
				:style="{'width': imgSize, 'height': imgSize}"
				@click="attchChoose.onClick">
				<text class="icon iconfont yn-plus"></text>
			</view>
		</view>
	</view>
</template>
<script>
	import { Base_Api,dev } from '@/common/config.js'
	
	export default {
		props: {
			parentPadding: {
				type: Number,
				default: 60
			},
			required: {
				type: Boolean,
				default: false
			},
			fileType: {
				type: String,
				default: "",
			}
		},
		data() {
			return {
				fileSrc: ""
			}
		},
		computed: {
			imgSize() {
				/**
				 * this.parentPadding 父容器的padding左右之和
				 * 2*21:.photo的margin-right: 21rpx(只有每一行的第一第二张图片有margin-right)
				 */
				return `calc((100vw - ${this.parentPadding+2*21}rpx)/3)`
			}
		},
		methods: {
			async upload(path) {
				  let str = path +''
				  let index = str.lastIndexOf("/")
				  let urlName = str.substring(index+1,str.length);
				  //ECU限制
				  if (!/\.(hex|s19)$/.test(urlName)) {
						uni.showToast({
						    title: '请上传正确格式',  
							duration: 2000, 
							icon : 'error'
						})
						return
				  } 
				  console.log(Base_Api)
					uni.showLoading({
						title: '上传中'
					});
				try {
					uni.uploadFile({
						url:‘’', // 上传接口地址
						filePath: path, // 本地路径
						name: 'file', // 后端接收文件的字段名
						formData: {
							fileName:urlName
						},
						header: {
							// "Content-Type":"application/json"
						},
						success: (res) => {
							console.log(res)
							if (res.statusCode == 200) { 
								uni.hideLoading();
								console.log("上传成功了");
								uni.showToast({
								 title: '上传成功了',   //  title 表示要显示文字内容
								 duration: 2000,  // 设置显示的时间  
								 icon : 'success'   // 设置显示的图标    success  成功    error 失败  none 不显示 
								})
								this.$emit('file', JSON.parse(res.data))
							} else {
								throw res
							}
						},
						fail: (err) => {
							// reject(err);
							uni.hideLoading();
							console.log(err);
								uni.showToast({
								 title: '上传失败了',   //  title 表示要显示文字内容
								 duration: 2000,  // 设置显示的时间  
								 icon : 'error'   // 设置显示的图标    success  成功    error 失败  none 不显示 
								})
							throw err
						}
					});
				} catch (e) {
					console.log(e);
					uni.showToast({
					title: '上传失败了',   //  title 表示要显示文字内容
					duration: 2000,  // 设置显示的时间  
					icon : 'error'   // 设置显示的图标    success  成功    error 失败  none 不显示 
					})
				}
			},
			async chooseFile(data) {
				try {
					// this.fileSrc = data.base64Str;
					const fileUrl = await this.base64toPath(data.base64Str, data.attachName);
					console.log(fileUrl);
					this.upload(fileUrl.relativePath)
				} catch (e) {
					console.log("err", e);
				}
			},
			/**
			 * @param {Object} base64 文件base64
			 * @param {Object} attachName //文件名需要后缀,如:张三.jpg
			 */
			async base64toPath(base64, attachName) {
				let _that = this;
				return new Promise(function(resolve, reject) {
					const filePath = `_doc/yourFilePath/${attachName}`;
					plus.io.resolveLocalFileSystemURL('_doc', function(entry) {
						entry.getDirectory("yourFilePath", {
							create: true,
							exclusive: false,
						}, function(entry) {
							entry.getFile(attachName, {
								create: true,
								exclusive: false,
							}, function(entry) {
								entry.createWriter(function(writer) {
									writer.onwrite = function(res) {
										const obj = {
											relativePath: filePath,
											localAbsolutePath: plus.io
												.convertLocalFileSystemURL(
													filePath)
										}
										resolve(obj);
									}
									writer.onerror = reject;
									writer.seek(0);
									writer.writeAsBinary(_that
										.getSymbolAfterString(base64,
											','));
								}, reject)
							}, reject)
						}, reject)
					}, reject)
				})
			},
			// 取某个符号后面的字符
			getSymbolAfterString(val, symbolStr) {
				if (val == undefined || val == null || val == "") {
					return "";
				}
				val = val.toString();
				const index = val.indexOf(symbolStr);
				if (index != -1) {
					val = val.substring(index + 1, val.length);
					return val;
				} else {
					return val
				}
			}
		}
	}
</script>
<script module="attchChoose" lang="renderjs">
	let fileInputDom = null;
	export default {
		methods: {
			createFileInputDom() {
				fileInputDom = document.createElement("input");
				fileInputDom.setAttribute('type', 'file');
				fileInputDom.setAttribute('accept', '*');
			},
			onClick(event, ownerInstance) {
				if (!fileInputDom) {
					this.createFileInputDom();
				}
				fileInputDom.click(); // 模拟click
				fileInputDom.addEventListener('change', (e) => {
					fileInputDom = null;
					let choicesFiles = e.target.files[0];
					let reader = new FileReader();
					//读取图像文件 result 为 DataURL, DataURL 可直接 赋值给 img.src
					reader.readAsDataURL(choicesFiles);
					reader.onload = function(event) {
						const base64Str = event.target.result; // 文件的base64
						ownerInstance.callMethod('chooseFile', {
							attachName: choicesFiles.name,
							size: choicesFiles.size,
							base64Str,
						})
					}
					e.target.value = "";
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.photo-control {
		display: flex;
		flex-flow: column nowrap;

		.photo-header {
			text {
				font-size: 24rpx;
			}
		}

		.photo-container {
			display: flex;
			flex-flow: row wrap;
			margin-top: 16rpx;

			.photo {
				&:not(:nth-child(3n)) {
					margin-right: 21rpx;
				}
				
				display: flex;
				flex-flow: column nowrap;
				justify-content: center;
				align-items: center;
				position: relative;
				margin-bottom: 20rpx;

				image {
					width: 100%;
					height: 100%;
				}
				
				.img-clear {
					position: absolute;
					top: 0;
					right: 0;
					background: #7C7F85;
					width: 36rpx;
					height: 36rpx;
					border-radius: 5rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #FFFFFF;
					
					.icon {
						font-size: 32rpx;
					}
				}

				&.plus {
					background-color: #f6f6f6;

					.iconfont {
						color: #bbb;
						font-size: 60rpx;
					}

					text {
						color: #999;
					}
				}
			}
		}
	}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值