轻松实现图片上传,只用 2kb大小不需要框架(如何不让图片自动上传)

网友投稿 644 2022-10-12

轻松实现图片上传,只用 2kb大小不需要框架(如何不让图片自动上传)

轻松实现图片上传,只用 2kb大小不需要框架(如何不让图片自动上传)

simpleGallery.js

Easy gallery upload. Native. Just 2kb gzipped.

Why

Upload, surely, is a hard part in programming, probably you have had problems with that. Why not simplify? No frameworks, just 2kb gzipped!

Working

Flow

When you send images and is returned a JSON data, it is saved in hidden input value. Same occurs when you edit title, reorder and remove images. After all actions, you just need to save that JSON in your database submitting the form.

Dependencies

To reorder gallery, is required Sortable, "a minimalist JavaScript library".

Install

You can get it on npm.

npm install simple-gallery-js --save-dev

Or bower, too.

bower install simple-gallery-js --save-dev

If you're not into package management, just download a ZIP file.

Setup

First, include the script located on the dist folder.

Now, you need to instantiate it.

new SimpleGallery('.form-upload');

Usage

When you upload images, your server needs to return a JSON data like:

[{ "url": "http://../image1.jpg" }, { "url": "http://../image2.jpg" }, ..]

Every data will be saved into a value of any hidden input, by default:

Lastly, you also need to create a div that will contain the gallery.

If you are not happy with default, change it!

// 1. Form container.// 2. Name of hidden input that will receive gallery data.// 3. Gallery container.new SimpleGallery('.form-upload', 'gallery', '.gallery-container');

Cheat

Upload

Advanced Options

If you don't want use same URL for upload images, add a data-action-gallery.

Browser Support

ChromeFirefoxIEOperaSafari
29+ 46+ 11+ Nope ✘9.1+ ✘

License

MIT LICENSE

Copyright 2016-2016 Fabio Carvalho fccoelho7@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Aha!设计模式(47)-适配器(4)
下一篇:Springboot+Mybatis实现分页加条件查询功能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~