web端即时聊天选择文件时向输入款生成文件缩略图,并实现点击缩略图模拟文件的下载

生成文件缩略图

//生成文件预览图
	function drawFileImg(fileName,fileUrl,fileSize){
//生成文件预览图
	function drawFileImg(fileName,fileUrl,fileSize){
		var myCanvas = $("#myCanvas")[0];
        var context = myCanvas.getContext("2d");
        var suffix = fileName.substring(fileName.lastIndexOf("."),fileName.length);
        var img = document.getElementById("otherIoc");
        if (suffix === ".doc" || suffix === ".docx") {
        	img = document.getElementById("wordIoc");
		} else if (suffix === ".xls" || suffix === ".xlsx") {
			img = document.getElementById("excleIoc");
		} else if (suffix === ".pdf") {
			img = document.getElementById("pdfIoc");
		}
        
        context.drawImage(img,10,15,80,120);
        context.font = "32px normal";
        var fileNameAbbreviation = "";
        if(fileName.length > 15){
        	fileNameAbbreviation = fileName.substring(0,9)+"...";
        }else{
        	fileNameAbbreviation = fileName;
        }
        context.fillText(fileNameAbbreviation,100,80,200);
        var fileSizeStr = fileSize.toString();
        if (fileSizeStr.length > 3) {
        	fileSizeStr = fileSizeStr.substring(0,fileSizeStr.length-3)+"."+fileSizeStr.substring(fileSizeStr.length-2,fileSizeStr.length);
		}else{
			fileSizeStr = "0."+fileSizeStr;
		}
        fileSizeStr = fileSizeStr+"kB";
        context.fillText(fileSizeStr,200,120,60);
        var image = new Image();
        //canvas转img
        image.src = myCanvas.toDataURL("image/png");
      	//清空canvas
        context.clearRect(0,0,myCanvas.width,myCanvas.height); 
        image.className = "fileCanvasImgClass";
        image.setAttribute("onclick","downloadFile('"+fileName+"','"+fileUrl+"')");
        image.setAttribute("type","fileImage");//增加自定义标识,用作分割判断
        $("#inp1"+chat_id).append(image);
	}

点击文件预览图模拟下载文件

//点击文件预览图模拟下载文件
	function downloadFile(name,url){
//点击文件预览图模拟下载文件
	function downloadFile(name,url){
		var a = $('<a>',{
       	   text:"111",
       	   href:url,
       	   download:name,
       	   target:"_blank"
       	});
		a[0].click();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值