解决uni.chooseimage,sourcetype无效的问题

1,253 阅读1分钟

需要实现一个仅用相机上传的功能,发现uni的sourcetype在web端不生效,于是用条件编译操作dom实现

//#ifdef H5

        const input = document.createElement("input");

        input.type = "file";

        input.accept = "image/*";

        input.capture = "camera";

        // safari 不把dom添加到文档树里面,不会触发onchange事件

        input.style.display = "none";

        document.body.appendChild(input);

        input.onchange = () => {

          const file = input.files[0];

          this.fileName = file.name;

          this.doUpload(file); // 上传文件方法

          document.body.removeChild(input);

        };

        input.click();

        //#endif

参考文章:

stackoverflow.com/questions/4…

在简书另外一篇文章的基础上加了兼容ios的代码,回过头来暂时没找到那篇文章的链接...