自定义图片视频上传插件

本文介绍了一款自定义开发的图片视频上传插件,该插件支持图片和视频的添加、移除及上传,并且带有进度条显示。通过使用websocket技术进行图片分片上传,最终返回图片在服务器上的实际名称。文章提供了关键代码示例,包括html、js、css以及后端相关Java类。
摘要由CSDN通过智能技术生成

由于项目需要,自定义开发了一个图片视频上传的插件

图片视频添加、移除、上传、进度条。

主要采用websocket实现图片分片上传和进度条。

返回结果为图片服务器中保存的图片的实际名称。

效果图:

关键代码:

index.html

$("#btn").click(function(){
	var photovideo = photoVideo();
	photovideo.openUploadWindow();
})

photoVideo.js 


var photoVideo = (function(windows){
	
	var photoVideo = function(){
		return new photoVideo.fn.init();
	}
	
	photoVideo.fn = photoVideo.prototype = {
		
		constructor:photoVideo,
		init: function(){
			this.serverUrl = "http://gdt.xxz.gov.cn:8015"; //接收图片服务地址
			this.fileSplitSize = 50000; //资源分片大小
			this.stompClient = {};	//stom客户
			this.uploadStatus = false;//上传状态
			this.uploadList = [];//上传列表
			this.resultList = [];//上传成功结果列表
			this.uuid = this.getUuid();//生成唯一标识
			this.webSocketConnect();//建立webSocket链接并获取链接唯一标识
			this.key = "";//传输通道标识
		},
		openUploadWindow: function(){	//设置图片和视频上传的窗口显示
			var me = this;
			$("body").append('<div class="upload_background">'+
								'<div class="upload_window">'+
									'<div class="upload_title">'+
										'图片视频上传'+
									'</div>'+
									'<div class="upload_content">'+
										'<div class="photo_area">'+
											'<div class="pv_title">图片</div>'+
											'<div id="photo_content" class="pv_content">'+
												'<div class="pv_add">'+
													'<input class="pv_input" type="file" name="" id="uploadImg" multiple accept="image/*">'+
												'</div>'+
											'</div>'+
											
										'</div>'+
										'<div class="video_area">'+
											'<div class="pv_title">视频</div>'+
											'<div id="video_content" class="pv_content">'+
												'<div class="pv_add">'+
													'<input class="pv_input" type="file" name="" id="uploadVideo" multiple accept="video/*">'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>'+
									'<div class="upload_btn">上传图片&视频</div>'+
								'</div>'+
							'</div>');
			//点击标题栏关闭
			$(".upload_title").click(function(){
				if(me.uploadStatus){
					alert("正在上传请勿操作");
					return;
				}
				me.closeUploadWindow();
			});
			//图片上传点击事件
			$("#uploadImg").change(function(){
				me.readFile(this,"img");
			});
			//视频上传点击事件
			$("#uploadVideo").change(function(){
				me.readFile(this,"video");
			});
			//上传
			$(".upload_btn").on("click",function(){
				if(me.uploadStatus){
					alert("正在上传请勿重复提交");
					return;
				}
				me.upload();
			});
			
		},
		closeUploadWindow: function(){	//关闭上传接口
			var me = this;
			var res = true;
			if(me.resultList.length != 0){
				res = confirm("退出将不保留已经上传的记录!");
			}
			if(res){
				me.init();  //关闭前重新初始化
				$(".upload_background").remove();//关闭窗口
			}
		
			
		},
		readFile: function(obj,type){//处理图片并添加都dom中的函数
			var me = this;
			// 获取input里面的文件组
			var fileList = obj.files;
			if(($("#photo_content>div").each(function(){}).length + fileList.length > 4&&type=="img")||($("#video_content>div").each(function(){}).length + fileList.length > 4&&type=="video")){
				alert("最多选择三张"+type);
				return;
			}
			//对文件组进行遍历,可以到控制台打印出fileList去看看
			for(var i=0;i<fileList.length;i++){
				var reader= new FileReader();
				reader.readAsDataURL(fileList[i]);
				(function(_i){ 
					// 当文件读取成功时执行的函数
					reader.onload=function(e){
						//将结果添加到待上传列表中
						var url = "'"+URL.createObjectURL(fileList[_i])+"'";
						me.uploadList.push({
							name: fileList[_i].name,
							size: fileList[_i].size,
							data: this.result,
							type: type,
							url: url
						});
						if(type == "img"){
							$("#photo_content").prepend('<div class="pv_photo"><span data-name="'+fileList[_i].name+'"></span><img src="'+this.resul
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值