全国统一服务热线:400-633-9193

js图片上传的封装代码

    网络     2017-09-05    1259

本文实例为大家分享了js图片上传的具体代码,供大家参考,具体内容如下

js封装的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

1
<div id="zTu">图片</div>

javascript

1
2
3
4
5
6
7
8
9
var coverImage;
initImageUploader();
function initImageUploader(){
  var fileUrl = 你想上传的地址;
  uploadImages('#imagePicker', fileUrl, function(file, res) {
   coverImage = res.url
   $('#zTu').get(0).innerHTML = '<div class="file-item thumbnail"><img style="max-height:180px" src="' + res.url + '" /></div>'
  })
 }

以上就是本文的全部内容,希望对大家的学习有所帮助


  分享到:  
0.4403s