关于本地图片在HTML页面上不能显示的问题

报错信息 GET file:///C:/fakepath/delete.png net::ERR_FILE_NOT_FOUND

这是浏览器安全机制的问题,获取不到图片的真实路径

目前网上的解决办法多半只兼容IE和火狐,而且不知是否可行

这个解决方法是原生js,不用导库,亲测可用

解决办法:

function getObjectURL(file) {
        var url = null;
        if (window.createObjcectURL != undefined) {
          url = window.createOjcectURL(file);
        } else if (window.URL != undefined) {
          url = window.URL.createObjectURL(file)
        } else if (window.webkitURL != undefined) {
          url = window.webkitURL.createObjectURL(file);
        }
        return url;
      }
      var objURL = getObjectURL(item.files[0]);
      console.log(objURL)
 this.img = objURL

item.files数据类似这样

关于本地图片在HTML页面上不能显示的问题_第1张图片

 

你可能感兴趣的:(H5)