You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
asyncfunctiongetPdfBinary(url){returnnewPromise((resolve,reject)=>{varxhr=newXMLHttpRequest();xhr.open("GET",url,true);xhr.responseType="arraybuffer";// get the binary xhr.setRequestHeader('content-type','application/json');xhr.onload=function(event){vararrayBuffer=xhr.response;varbyteArray=newUint8Array(arrayBuffer);varlen=byteArray.byteLength;varbinary=""for(vari=0;i<len;i++){binary+=String.fromCharCode(byteArray[i]);}resolve(binary);}xhr.send();})}
constdownloadPdfForIos=async(pdf)=>{consturl="data:application/pdf;base64,"+pdf;constshareOptions={title: 'image report',failOnCancel: false,saveToFiles: true,url: url,// base64 with mimeType or path to local file};try{constShareResponse=awaitShare.open(shareOptions);Alert.alert(null,I18n.t("myHealth.Downloaded"),[{text: I18n.t("common.OK"),onPress: ()=>null}]);}catch(error){if(error.error&&error.error.code==="ECANCELLED500"){console.warn("canceled");}else{Alert.alert(null,I18n.t("myHealth.DownloadFailed"),[{text: I18n.t("common.OK"),onPress: ()=>null}]);}}}
The text was updated successfully, but these errors were encountered:
很久就实现过的功能,今天又遇到了,竟然无从下手,只好去翻翻旧代码并在这里记录一下也方便日后查看。
需求分析
1. 首先获取pdf文件二进制
2. 转成base64字符串
3. 下载到android手机
今天刚好在stackoverflow上回答别人的问题,他需要的是expo,所以写了个expo版本的。Base64 String to pdf JavaScript React Native then download
For expo
For android
4. 下载到ios手机
ios不能直接下载文件到外部app,只能通过分享到file app实现。找了很久没找到其他办法,有其他办法的大神请多多指教。
The text was updated successfully, but these errors were encountered: