Git Product home page Git Product logo

upload-image's Introduction

Upload-Image

Upload Image Plugin by Javascript
可预览图片、多张上传、拖拽上传

立即使用

<body>
	<form action="#">
		<div id="box"></div>
	</form>
	<script type="text/javascript" src="./upload_image.js"></script>
	<script type="text/javascript">
		let upload = new Upload(document.getElementById('box'));
	</script>
</body>

options参数

Example:

let upload = new Upload(document.getElementById('box'),{
  multiple : true,
  max : 3
});

Options List:

  • multiple:false 是否使用多图模式(与max项配合使用)
  • max:1 最大图片张数
  • size:"1MB" 每张图片限制大小

方法

let oFiles = upload.getFiles();
let aSizes = upload.getSizes();

Method List:

  • getFiles:获取文件列表对象,里面包含了所有显示的图片,可用于构建FormData
  • clearFiles:清空文件列表对象,并清除所有显示的图片
  • getSizes:获取保存图片大小的数组
  • getExtensions:获取拓展名数组(当前['jpg','jpeg','gif','png','ico','dwg','bmp']
  • addExtensions:添加拓展名数组,以['abc']形式添加图片文件的拓展名,所有拓展名会转换为小写

其他

  • 初始化Upload时,会自动创建一个id为file_btn的file按钮,如果你不希望看到这个丑丑的按钮,你可以将file_btndisplay设为none,并设置box的onclick事件,在该事件中模拟点击按钮
document.getElementById('box').onclick = function(){
  document.getElementById('file_btn').click();
}
  • 如果你希望在拖拽图片进入box的时候让它有一些高亮效果,可以设置box的.active的样式,这个classname是动态的,在拖拽进入box时被添加,离开box时被移除
  • 每张预览的图片都有一个classname,为upload-img

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.