七牛,前端上传图片

文本框:

<input type="file" class="file-picker" multiple accept="image/*" @change="selectImg">

调用函数:

selectImg: function(e){
                    var that = this;
                    var files = e.target.files;
                    for(var i = 0; i < files.length; i++){
                        that.upImg(files[i], function(path){
                            that.comment.pics.push(path);
                        });
                    }
                },
                upImg: function(file, callback){
                    var that = this;
                    that.fileToken(function(access_token){
                        var form = new FormData();
                        form.append('file', file);
                        form.append('token', access_token);
                        $.ajax({
                            // url: 'http://up-z2.qiniu.com/',
                            url: (window.location.protocol === 'https:')? 'https://upload-z2.qbox.me': 'http://upload-z2.qiniu.com',
                            data: form,
                            processData: false,
                            contentType: false,
                            type: 'POST',
                            success: function(data){
                                console.log(data);
                                var path = 'https://img.yishou520.com/' + data.key;
                                callback(path);
                            }
                        });
                    });
                },
                // 获取七牛上传token
                fileToken: function(callback){
                    var that = this;
                    that.fetch({
                        url: 'qiniu/token',
                        success: function(access_token){
                            callback(access_token);
                        }
                    })
                }

猜你喜欢

转载自www.cnblogs.com/lixingbaophp/p/9159830.html
今日推荐