This article mainly introduces the encapsulation code of js image uploading in detail. It has certain reference value. Interested friends can refer to it.
The example of this article shares with you the js image uploading method. The specific code is for your reference. The specific content is as follows
js encapsulation method
function uploadImages(picker, url, callback) { var img_uploader = WebUploader.create({ auto: true, server: url, pick: picker, fileNumLimit: 1, fileSingleSizeLimit: 2097152, // 2M accept: { title: 'Images', extensions: 'gif,jpg,jpeg,bmp,png', // mimeTypes: 'image/*' }, compress: { width: 300, compressSize: 102400 // < 100kb 不压缩 }, }) var fileType = ['image/jpeg', 'image/jpg', 'image/gif', 'image/png', 'image/bmp'] img_uploader.on('beforeFileQueued', function(file) { fileType.some(function(name) { return file.type === name }) ? '' : alert('请上传正确的图片!') }) img_uploader.on('uploadSuccess', function(file, res) { callback(file, res) }) img_uploader.on('uploadError', function(file, reason) { console.log(reason); }) img_uploader.on('uploadComplete', function(file) { img_uploader.reset() }) }
html
图片
javascript
var coverImage; initImageUploader(); function initImageUploader(){ var fileUrl = 你想上传的地址; uploadImages('#imagePicker', fileUrl, function(file, res) { coverImage = res.url $('#zTu').get(0).innerHTML = '' }) }
The above is the detailed content of JavaScript image upload code encapsulation. For more information, please follow other related articles on the PHP Chinese website!