Newer
Older
DH_Apicture / src / directive / el-downLoad / index.js
@zhangqy zhangqy on 29 Nov 1 KB first commit
  1. //屏蔽右键功能
  2. // 添加自定义v-down指令
  3. // let baseDownloadUrl = process.env.VUE_APP_BASE_API; // 域名
  4. export default {
  5. mounted(el, binding, vnode) {
  6. const { value } = binding;
  7. el.style.cssText = "cursor: pointer;color:#1890ff;";
  8. el.style.width = "100%";
  9. el.addEventListener("click", () => {
  10. console.log(binding);
  11. let link = document.createElement("a"); // 创建a标签
  12. link.style.display = "none";
  13. let url = ""; //文件地址
  14. let name = ""; //文件名称
  15. // 如果指令绑定的值为对象则取名称和地址,否则直接取值(即地址)
  16. if (binding.value instanceof Object) {
  17. url = binding.value.scanAttachUrl;
  18. name = binding.value.scanAttachName;
  19. } else {
  20. url = binding.value; // 设置下载地址
  21. }
  22. // 这里是将url转成blob地址,
  23. fetch(url)
  24. .then((res) => res.blob())
  25. .then((blob) => {
  26. // 将链接地址字符内容转变成blob地址
  27. link.href = URL.createObjectURL(blob);
  28. if (binding.value instanceof Object) {
  29. link.download = name; //取名称
  30. } else {
  31. link.download = url.split("/")[url.split("/").length - 1]; //取名称
  32. }
  33. document.body.appendChild(link);
  34. link.click();
  35. });
  36. });
  37. },
  38. };