Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

为什么七牛上传图片会报错

图片描述

var uploader = Qiniu.uploader({
        runtimes: 'html5,flash,html4',
        browse_button: 'pickfiles',
        container: 'container',
        drop_element: 'container',
        max_file_size: '1000mb',
        flash_swf_url: 'bower_components/plupload/js/Moxie.swf',
        dragdrop: true,
        chunk_size: '4mb',
        multi_selection: !(mOxie.Env.OS.toLowerCase()==="ios"),
          uptoken_func: function(){
          $.ajax({
            url:'/qiniuUpToken',
            method:'post',
            success:function(data){
              console.log(data)
              $('#domain').val(data.data.domain);
              console.log('uptoken_func:' + data.data.uptoken);
              return data.data.uptoken;
            }
          })
          },
          multi_selection: false,
          unique_names: false,
          save_key: false,
        domain: $('#domain').val(),
        get_new_uptoken: false,
        auto_start: true,
        //unique_names: true,      //设置所有文件名唯一
        filters: {  
            mime_types : [ //只允许上传图片  
                { title : "Image files", extensions : "jpg,jpeg,gif,png" },  
            ],  
            prevent_duplicates : false //不允许选取重复文件  
        },  
        log_level: 5,
        init: {
            'Key': function(up, file) {
              var now = new Date();
                    var date = now.getFullYear()+"/"+((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1)+"/"+(now.getDate()<10?"0":"")+now.getDate();
                    return "works/"+date+"/"+getFileName(file.name);
              },
          'FilesAdded': function(up, files) {
              $('table').show();
              $('#success').hide();
              plupload.each(files, function(file) {
                var reader = new FileReader();
                      reader.readAsDataURL(files[0].getNative());
                      reader.onload = (function (e) {
                        var image = new Image();
                        image.src = e.target.result;
                        image.onload = function () {
                            file.width = this.width;
                            file.height = this.height;
                        };
                      });
                var progress = new FileProgress(file, 'fsUploadProgress');
                    progress.setStatus("等待...");
                    progress.bindUploadCancel(up);
              });
                //图片上传前预览
                      for (var i = 0; i < files.length; i++) {
                var fileItem = files[i].getNative(),
                  url = window.URL || window.webkitURL || window.mozURL;
                  var src = url.createObjectURL(fileItem);
                  var tmpl = '<li style="background-image:url(#url#)"  data-url="#url#"><span></span></li>';
                  $("#fsUploadProgress").append($(tmpl.replace(/#url#/ig, src)));
              }
                      var sss = uploader.files
                      console.log(sss.length,'哈哈哈哈哈哈哈哈哈');
                      if(sss.length>9) { // 最多上传3张图
                          alert('最多只能上传9张图');
                            uploader.removeFile(files[0]);
                  var i = 0;
                  $('.img_area li').each(function(){
                      //console.log('进来')
                      i++
                  })
                  //var length = $(this).length+1;
                    console.log(i);
                    if (i>18) {
                        //console.log('进来了')
                        $(".img_area li:gt(17)").remove();
                    }
                  return;
                }
            },
            'BeforeUpload': function(up, file) {
                console.dir(up)
                var progress = new FileProgress(file, 'fsUploadProgress');
                var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
                if (up.runtime === 'html5' && chunk_size) {
                    progress.setChunkProgess(chunk_size);
                }
            },
            'UploadProgress': function(up, file) {
                var progress = new FileProgress(file, 'fsUploadProgress');
                var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
                progress.setProgress(file.percent + "%", file.speed, chunk_size);
            },
            'UploadComplete': function() {
              console.log('成功上传')
                
            },
            'FileUploaded': function(up, file, info) {
                console.log('FileUploaded仅仅是:'+info)
                var progress = new FileProgress(file, 'fsUploadProgress');
                info = eval('('+info+')');
                var imgKey = encodeURI($("#domain").val()+info.key);
                console.log('imgKey:'+imgKey)
                
            },
            'Error': function(up, err, errTip) {=
                var progress = new FileProgress(err.file, 'fsUploadProgress');
                progress.setError();
                progress.setStatus(errTip);=
            }
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

看反馈的错误信息,应该是获取token时的上传策略,和实际上传时的参数不一致。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...