账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    js不能上传重复图片
    31
    0
    上传图片的功能不能上传两张一样的图片,方法如下,感谢各位 openImg(item2, el, index, field, indexId, level, type) { ;(this.clickTagValue = item2), (this.clickTagField = field), (this.clickTagIndexId = indexId), (this.clickTagLevel = level), (this.clickTagType = type), (this.isArrayclickTagType = type), localStorage.setItem('skuTable', JSON.stringify(this.skuTable)) console.log( item2, el, index, field, indexId, level, type, 'ooooooooooooooo' ) this.imgsObj[item2.specItem] = item2.image // console.log(item2.specItem,index,'item2222222222'); this.chooseImgItem = item2.specItem this.chooseIndex = index this.imgLoopList = item2 this.apiImgLoopList = item2 if (inputElement === null) { // 生成文件上传的控件 inputElement = document.createElement('input') inputElement.setAttribute('type', 'file') inputElement.style.display = 'none' if (window.addEventListener) { inputElement.addEventListener('change', this.uploadFile, false) } else { inputElement.attachEvent('onChange', this.uploadFile) } document.body.appendChild(inputElement) // this.createTableListRowData() } inputElement.click() // this.createTableListRowData(); }, uploadFile(el, value, field, indexId, level, type) { console.log( el, value, field, indexId, level, type, 'uploadfile===========' ) if (el && el.target && el.target.files && el.target.files.length > 0) { this.files = el.target.files[0] const files = el.target.files[0] const isLt2M = files.size / 1024 / 1024 < 2 const size = files.size / 1024 / 1024 console.log(size) // 判断上传文件的大小 if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!') } else if (files.type.indexOf('image') === -1) { //如果不是图片格式 // this.$dialog.toast({ mes: '请选择图片文件' }); this.$message.error('请选择图片文件') } else { let that = this const reader = new FileReader() // 创建读取文件对象 reader.readAsDataURL(el.target.files[0]) // 发起异步请求,读取文件 // console.log(window.URL.createObjectURL(files),'files1111111111111111111111') // this.previewImage = window.URL.createObjectURL(files); reader.onload = function() { // 文件读取完成后 // 读取完成后,将结果赋值给img的src that.imgLoopList.image = URL.createObjectURL(files) that.uploadType = '图片上传完成' // that.clickTag( // that.clickTagValue, // that.clickTagField, // that.clickTagIndexId, // that.clickTagLevel, // that.clickTagType, // that.uploadType // ) } } } },
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 向前冲 普通会员 1楼

      在JavaScript中,你可以使用HTML5的FileReader API来上传图片,但这个API并不能确保图片不会被上传重复。在文件上传过程中,浏览器会自动检测并去除重复的文件。

      如果你需要确保图片不会被重复上传,你可能需要使用一些库或者框架。例如,你可以使用Node.js的multer库来处理文件上传,multer支持的图片上传规则非常严格,可以避免重复上传。

      另外,你还可以在服务器端检查上传的图片,如果发现已经存在相同的图片,你可以选择拒绝上传或者删除上传的文件。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部