file.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { getToken } from '@/utils/auth'
  2. import $qs from 'qs'
  3. export function fileDown(url, params = {}, name = '') { // 导出
  4. const link = document.createElement('a') // 创建事件对象
  5. const query = $qs.stringify(Object.assign({}, {
  6. token: getToken()
  7. }, params))
  8. link.setAttribute('href', process.env.VUE_APP_BASE_API2 + '/' + url + '?' + query)
  9. link.setAttribute('download', name)
  10. link.setAttribute('target', '_blank')
  11. const event = document.createEvent('MouseEvents') // 初始化事件对象
  12. event.initMouseEvent('click', true, true, document.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 0,
  13. null) // 触发事件
  14. link.dispatchEvent(event)
  15. }
  16. export function UploadImg(file, size) { // size 单位为M
  17. const isImage = (file.type === 'image/jpeg' ||
  18. file.type === 'image/gif' ||
  19. file.type === 'image/png' ||
  20. file.type === 'image/bmp')
  21. const isLt2M = file.size / 1024 / 1024 < size
  22. if (!isImage) {
  23. this.$message.error('上传图片只能是 JPG、JPEG、GIF、PNG、BMP 格式!')
  24. }
  25. if (!isLt2M) {
  26. this.$message.error(`上传图片大小不能超过 ${size}MB!`)
  27. }
  28. if (isImage && isLt2M) {
  29. const param = new FormData()
  30. param.append('file', file, file.name)
  31. // API.file.upload(param, (res) => {
  32. // if (res.status) {
  33. // this.ruleForm.demandHeadImg = res.data.url
  34. // }
  35. // })
  36. }
  37. }