uniapp-实现图片上传,剪切功能

效果图

点击加号,选择图片

点击右下角确定

代码展示

将uni-file-picker组件和ksp-cropper组件修改,二次封装

(在上传时,使用了我自己的接口,可替换)

uni-file-picker组件和ksp-cropper组件下载地址,如下:

uni-file-picker 文件选择上传 - DCloud 插件市场

ksp-cropper - DCloud 插件市场

组件代码

<template>
	<view>
		<uni-file-picker :limit="limit" mode="grid" :fileMediatype="fileMediatype" :file-extname="fileType"
			@progress="progress" @success="success" @fail="fail" @select="select" @delete="delIMG"
			v-model="dataList"></uni-file-picker>
		<ksp-cropper v-if="localDataShowCropper" class="crop" mode="fixed" :width="250" :height="250" :maxWidth="1024"
			:maxHeight="1024" :url="currentImageUrl" @cancel="handleCancel" @ok="handleCropOk"></ksp-cropper>
	</view>
</template>
<script>
	export default {
		name: "sk-upload-crop",
		props: {
			limit: {
				type: Number,
				default: 1
			},
			fileType: {
				type: Array,
				default: function() {
					return []
				}
			},
			type: {
				type: Number
			},
			// 默认数据,逗号隔开(用于保存和回显数据)
			modelValue: {
				type: String,
				default: ''
			},

			showCropper: {
				type: Boolean,
				default: false
			}
		},

		data() {
			return {
				// 用于展示数据
				dataList: [],
				fileMediatype: 'all',
				currentImageUrl: '',
				localDataShowCropper: this.showCropper,
			};
		},

		created() {
			let _this = this
			const temList = _this.$util.isNull(_this.modelValue) ? [] : _this.modelValue.split(',')
			temList.forEach((item, i) => {
				_this.dataList.push({
					'url': _this.$config.getConfig().fileBasePath + item
				})
			})
			if (_this.fileType.indexOf("png") > -1) {
				_this.fileMediatype = "image"
			}
		},

		methods: {
			select(e) {
				const tempFilePaths = e.tempFilePaths;
				if (tempFilePaths && tempFilePaths.length > 0) {
					this.currentImageUrl = tempFilePaths[0];
					this.localDataShowCropper = true;
				}
			},

			handleCropOk(currentImageUrl) {
				console.log('currentImageUrl',currentImageUrl)
				let _this = this
				let list = _this.$util.isNull(_this.modelValue) ? [] : _this.modelValue.split(',')
				let dataList = _this.dataList
				uni.uploadFile({
					url: _this.$config.getConfig().reqBasePath + 'common003',
					filePath: currentImageUrl.path,
					name: 'imgUrl',
					header: {
						"requestType": "2",
						"userToken": uni.getStorageSync("userToken")
					},
					formData: {
						type: 37,
					},
					success: (res) => {
						if (res.statusCode == 200) {
							var jsonData = JSON.parse(res.data);
							if (jsonData.returnCode == 0) {
								let bean = jsonData.bean
								dataList.push({
									'url': _this.$config.getConfig().fileBasePath + bean.picUrl
								})
								list.push(bean.picUrl)
								_this.dataList = dataList
								_this.$emit('update:modelValue', list.toString());
							} else {
								uni.showToast({
									icon: 'none',
									position: 'bottom',
									title: jsonData.returnMessage
								});
							}
						}
					}
				});
				this.localDataShowCropper = false;
			},

			handleCancel() {
				this.clearData()
			},

			clearData() {
				this.dataList = [];
				this.currentImageUrl = '';
			},

			// 图片删除
			delIMG(e) {
				// 删除显示的数据
				const num = this.dataList.findIndex(v => v.url === e.tempFilePath);
				this.dataList.splice(num, 1);
				// 删除保存的数据
				let list = this.$util.isNull(this.modelValue) ? [] : this.modelValue.split(',')
				const savePath = e.tempFilePath.replace(this.$config.getConfig().fileBasePath, '')
				const num1 = list.findIndex(v => v === savePath);
				list.splice(num1, 1);
				this.$emit('update:modelValue', list.toString());
			},

			// 获取上传进度
			progress(e) {
				console.log('上传进度:', e)
			},

			// 上传成功
			success(e) {
				console.log('上传成功')
			},

			// 上传失败
			fail(e) {
				console.log('上传失败:', e)
			}
		}
	}
</script>
<style scoped>
	::v-deep .is-add {
		background-color: white;
	}

	.crop {
		position: fixed;
		left: 0px;
		z-index: 100;
	}
</style>

父页面使用

<SkUploadCrop ref="uploadCrop" v-else class="pic" :type="37" :limit="1"
						:fileType="$fileType.imageType" :showCropper="isShowingCropper" v-model="formData.img">
					</SkUploadCrop>

  • 19
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Element Plus 中,要实现上传图片剪切图片功能,你可以按照以下步骤进行操作: 1. 首先,你需要在你的项目中安装 Element Plus。你可以通过 npm 或 yarn 来安装,具体安装方式可以参考 Element Plus 的官方文档。 2. 在你的组件中引入需要的组件和样式文件,例如: ```js import { Upload, Image, Dialog } from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; ``` 3. 在模板中使用 `Upload` 组件来实现图片上传功能,例如: ```html <Upload action="/upload" <!-- 上传图片的接口地址 --> :on-success="handleSuccess" <!-- 上传成功后的回调函数 --> > <template #default="{ file, fileList }"> <Image v-if="fileList.length > 0" :src="fileList[0].url" fit="contain" /> <div v-else class="el-upload-dragger"> <p class="el-upload-dragger__text">将文件拖到此处,或点击上传</p> </div> </template> </Upload> ``` 4. 在组件的 `methods` 中定义 `handleSuccess` 方法来处理上传成功后的逻辑,例如: ```js methods: { handleSuccess(response, file, fileList) { // 在上传成功后,弹出剪切图片的对话框 Dialog({ title: '剪切图片', width: '50%', component: YourCropComponent, // 自定义的剪切图片组件 props: { imageUrl: fileList[0].url // 将上传成功的图片地址传递给剪切图片组件 } }); } } ``` 5. 最后,你需要编写一个自定义的剪切图片组件 `YourCropComponent`,这个组件可以使用第三方库或自己实现图片剪切功能。 通过以上步骤,你可以在 Element Plus 中实现上传图片剪切图片功能。注意,剪切图片的具体实现需要根据你选择的第三方库或自己编写的组件来进行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值