用JS 七牛上传图片出现文件已存在的错误(file exists),可明明是第一次上传,而且我有更改key值,如果把本地文件名改了就又不会报错了
用官方的demo也会报错http://jssdk.demo.qiniu.io/#
var uploader_pub = Qiniu.uploader({ runtimes: 'html5,flash,html4', browse_button: 'public_pickfiles', container: 'container', drop_element: 'public_pickfiles', max_file_size: '15mb', flash_swf_url: 'bower_components/plupload/js/Moxie.swf', dragdrop: true, max_retries:3, chunk_size: '4mb', multi_selection: !(mOxie.Env.OS.toLowerCase()==="ios"), uptoken_func: function(){ }, multi_selection: true, unique_names: false, save_key: false, domain: $('#public_domain').val(), get_new_uptoken: false, auto_start: false, //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(); var lastimg="work/"+date+"/"+getFileName(file.name); return lastimg; }, 'FileUploaded': function(up, file, info) { var progress = new FileProgress(file, 'public_fs'); info = eval('('+info+')'); var imgKey = encodeURI($("#public_domain").val()+info.key); imageFile.push({image:imgKey+"",isLayer:false}) coverHash.push({hash:info.hash,key:info.key}) }, 'FilesAdded': function(up, files) { plupload.each(files,function(file,index) { var tempArr = file.name.split("."); var ext; if (tempArr.length === 1 || (tempArr[0] === "" && tempArr.length === 2)) { ext = ""; } else { ext = tempArr.pop().toLowerCase( ); //get the extension and make it lower-case } var now = new Date(); var date = now.getFullYear()+((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1)+(now.getDate()<10?"0":"")+now.getDate(); var keyName="work/"+date+"/"+getTimeHSS()+"/"+index+"."+ext; file.name=keyName; 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; }; }); }); //图片上传前预览 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 = ' '; $(".public_add_more").before($(tmpl.replace(/#url#/ig, src))); } if($('#public_fs li').length>0){ $('#public_fs').removeClass('img_area_back'); $('.public_add_more').css('display','block'); } if($('#public_fs li').length>=9){ $('.public_add_more').css('display','none'); } var sss = uploader_pub.files; console.log(sss) if(sss.length>9) { // 最多上传9张图 swal({ text: '最多只能上传9张图', showConfirmButton:false, target:'.mySwl_box', width:'auto', timer:1000, animation:false, }).catch(swal.noop) var i = 0; $('#public_fs li').each(function(){ i++ }) uploader_pub.files.splice(9,uploader_pub.files.length) if (i>8) { $("#public_fs li:gt(8)").remove(); } return; }else{ isRepeat(sss) } function isRepeat(arr){ for( var i=0;i255){ $('.loading').css('display','none'); swal({ text:'表情描述不能超过255个字~', showConfirmButton:false, target:'.mySwl_box', width:'auto', timer:1000, animation:false, }).catch(swal.noop) }else{ if (coverHash.length!=0 ) { if(imageFile.length==1){ var coverHash_one=coverHash[0].hash; $.ajax({ type:'post', url:'/setting/cheackHash', data:{hash:coverHash_one}, success:function(data){ if(data.code==200){ $('#pub_imageFile').val(images); $('#pub_coverHash').val(coverHash_one); cookieTopic(str_content) $.ajax({ type:'post', url:'/imgupload', data:{content:str_content,imagedata:images,coverHash:coverHash_one}, success:function(data){ $('.loading').css('display','none'); if(data.code==200){ swal({ text:'发布成功~', showConfirmButton:false, target:'.mySwl_box', width:'auto', timer:1400, animation:false, }).catch(swal.noop) //初始化 updataInit() //初始化 end$('.created-topic').fadeOut('fast') $('.fabu').fadeOut('fast') window.location.href='//m.sbmmt.com/user/'+userId }else{ coverHash =[]; imageFile = []; uploader_pub.files.splice(0,uploader_pub.files.length); $('.public_add_imgarea').find('.public_add_more').css('display','block') $('.public_add_imgarea>li').remove(); swal({ text: data.msg, showConfirmButton:false, target:'.mySwl_box', width:'auto', timer:1000, animation:false, }).catch(swal.noop) } $('.loading').css('display','none'); } }); }else{ $('.loading').css('display','none'); swal({ text:data.msg, showConfirmButton:false, target:'.mySwl_box', width:'auto', timer:1400, animation:false, }).catch(swal.noop) //初始化 updataInit() } }//success }) }else{ var coverHash_one=coverHash[0].hash; $('#pub_imageFile').val(images); $('#pub_coverHash').val(coverHash_one); cookieTopic(str_content) $.ajax({ type:'post', url:'/imgupload', data:{content:str_content,imagedata:images,coverHash:coverHash_one}, success:function(data){ if(data.code==200){ swal({ text:'发布成功~', showConfirmButton:false, target:'.mySwl_box', width:'auto', timer:1400, animation:false, }).catch(swal.noop) //初始化 updataInit() //初始化 end $('.fabu').fadeOut('fast') // window.location.href='//m.sbmmt.com/user/'+userId }else{ coverHash =[]; imageFile = []; uploader_pub.files.splice(0,uploader_pub.files.length); $('.public_add_imgarea').find('.public_add_more').css('display','block') $('.public_add_imgarea>li').remove(); swal({ text: data.msg, showConfirmButton:false, target:'.mySwl_box', width:'auto', timer:1000, animation:false, }).catch(swal.noop) } $('.loading').css('display','none'); } }); } } else{ $('.loading').css('display','none'); swal({ text: '请上传图片', showConfirmButton:false, target:'.mySwl_box', width:'auto', timer:1000, animation:false, }).catch(swal.noop) } } }, 'Error': function(up, err, errTip) { $('.loading').css('display','none'); console.log(err) } } });
可以看下这里:
https://github.com/qiniu/js-sdk