将文件流blob或文件file读取成url

let url = URL.createObjectURL(file.raw) //将文件转化成url

//或者

let url = URL.createObjectURL(blob) //将文件流转化成url  注意这个blob一般是通过读取后端接口文件流 设置 responseType: 'blob'  然后返回得到的blob
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在WebView中支持文件Blob),您需要按照以下步骤进行操作: 1. 将文件换为Blob对象。您可以使用FileReader API中的readAsArrayBuffer或readAsDataURL方法来读取文件并将其换为Blob对象。 ``` var xhr = new XMLHttpRequest(); xhr.open('GET', 'example.pdf', true); xhr.responseType = 'arraybuffer'; xhr.onload = function(e) { if (this.status == 200) { var blob = new Blob([this.response], {type: 'application/pdf'}); // Use the blob object to display the PDF in WebView } }; xhr.send(); ``` 2. 将Blob对象换为Base64编码的数据URL。您可以使用URL.createObjectURL方法将Blob对象换为URL,然后使用XMLHttpRequest或Fetch API将该URL传递给WebView。 ``` var xhr = new XMLHttpRequest(); xhr.open('GET', 'example.pdf', true); xhr.responseType = 'blob'; xhr.onload = function(e) { if (this.status == 200) { var blob = this.response; var reader = new FileReader(); reader.onloadend = function() { var base64data = reader.result; // Use the base64data to display the PDF in WebView }; reader.readAsDataURL(blob); } }; xhr.send(); ``` 3. 在WebView中加载Base64编码的数据URL。您可以使用WebView的loadData或loadDataWithBaseURL方法将Base64编码的数据URL加载到WebView中。 ``` webView.loadData(base64data, 'application/pdf', 'base64'); ``` 注意:在使用Blob对象和Base64编码的数据URL时,请确保WebView的JavaScriptEnabled属性设置为true,以允许JavaScript代码在WebView中运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值