龙空技术网

js html input file 类型 实现图片上传

白雨青 227

前言:

而今你们对“jquery获取图片类型”大体比较看重,我们都需要知道一些“jquery获取图片类型”的相关文章。那么小编同时在网络上汇集了一些有关“jquery获取图片类型””的相关内容,希望你们能喜欢,咱们一起来学习一下吧!

白雨青工作站发文地址:js html input file 类型 实现图片上传-白雨青工作站

js html input file 类型 实现图片上传

这里只单独讲如何用JS 把选中的文件转成base64 ,然后输出到前台显示

直接上代码:

AddArticle.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML><html>  <head>    <base href="<%=basePath%>">        <title>写文章-白雨青工作站</title>    	<meta http-equiv="pragma" content="no-cache">	<meta http-equiv="cache-control" content="no-cache">	<meta http-equiv="expires" content="0">    	<meta http-equiv="keywords" content="小说,我有一剑,Java,HTML,Java小工具,白雨青工作站,;>	<meta name="description" content="小说,我有一剑,Java,HTML,Java小工具,白雨青工作站,;>	<!---->		<link rel="shortcut icon" href="img/byqws-apple-touch-icon-16X16.png" />	<link rel="apple-touch-icon" href="img/byqws-apple-touch-icon-57X57.png" />	<link rel="apple-touch-icon" sizes="72x72" href="img/byqws-apple-touch-icon-72X72.png" />	<link rel="apple-touch-icon" sizes="114x114" href="img/byqws-apple-touch-icon-144X144.png" />		<link rel="stylesheet" type="text/css" href="Background/css/addArticle.css">			<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">	<script type="text/javascript" src="js/jquery-1.4.min.js"></script>			<script src="Background/js/tinymce/tinymce.min.js" type="text/javascript" charset="utf-8"></script>	<script src="Background/js/tinymce/langs/zh-Hans.js" type="text/javascript" charset="utf-8"></script> <!--汉化-->		<script type="text/javascript" src="Background/js/AddArticle.js"></script>	<script type="text/javascript" src="Background/js/mytinymce.js"></script>	<script type="text/javascript" src="Background/js/uploadImg.js"></script>	  </head>    <body>  <div class='main'>	<div class='title'>		<label >添加一篇文章</label>	</div>				<div class='FTitle_div'>		<input id="FTitle" class='FTitle' placeholder = '请输入标题'></input>	</div>		<div class='FAbstract_div'>		<input id="FAbstract" class='FAbstract' placeholder = '请输入摘要'></input>	</div>	<div class='content_div'>		<textarea id="content" class='content'></textarea>	</div>	<div class="FCover">封面图片</div>	<div class="upload-piclist">		       	<div class="upload-file">           <input type="file" id="file" accept="image/*" multiple onchange="imgChange()"/>       	</div>    </div>	<input id="submitbutton" class='submitbutton' type = 'button' value = '提交'></input></div>  </body></html>

<script type="text/javascript" src="Background/js/uploadImg.js"></script>

引入转base64后前台显示JS文件

uploadImg.js

let picmax = 20; //限制上传数量 function imgChange() {	let file = document.getElementById('file').files;	let imglist = document.querySelectorAll('.upload-Picitem');	let piclist = document.getElementsByClassName('upload-piclist')[0];	let filelist = file.length + imglist.length > picmax ? 9 - imglist.length : file.length + imglist.length;	if (file.length + imglist.length >= 9) {		let uploadfile = document.getElementsByClassName('upload-file')[0]		uploadfile.style.display = "none"	}	for (let i = 0; i < filelist; i++) {		readerfile(file[i]).then(e => {			let html = document.createElement('div');			html.className = 'upload-Picitem'			html.innerHTML = '<img src=' + e + ' alt="pic">'			piclist.appendChild(html);		})	}}function readerfile(file) {	return new Promise((resolve, reject) => {		let reader = new FileReader();		reader.addEventListener("load", function() {			resolve(reader.result);		}, false)		if (file) {			reader.readAsDataURL(file)		}	})}//提交function submit() {	let imglist = []	//let text = document.getElementsByClassName('upload-textarea')[0].value	let piclist = document.querySelectorAll('.upload-Picitem');	for (let i = 0; i < piclist.length; i++) {		imglist.push(piclist[i].lastChild.src)	}	//console.log("发布内容:", text)	console.log("图片列表:", imglist)}

直接看结果

标签: #jquery获取图片类型