在 iOS 上下载 .pkpass 文件时出现错误:“Safari 无法下载”和“此时无法安装到 Passbook”
const formBody = `paxList=${encodedPayLoad}`;
const response = await fetch(
"http://192.168.8.154:8080/accelaero/DownloadMobilePass",
{
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: formBody
});
if (!response.ok) {
window.alert(response);
throw new Error("Failed to save pk pass");
}
else {
const contentDisposition = response.headers.get('Content-Disposition');
let filename = "pass.pkpass"; // Default filename
if (contentDisposition && contentDisposition.includes('filename=')) {
const matches = contentDisposition.match(/filename="?([^";]+)"?/);
if (matches && matches[1]) {
filename = matches[1]; // Extracted filename
}
}
const rawBlob = await response.blob();
const blob = new Blob([rawBlob], { type: 'application/vnd.apple.pkpass' })
const downloadURL = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = downloadURL;
a.download = filename;
document.body.appendChild(a);
const proceedWithDownload = window.confirm(`Do you want to download the file: ${filename}?`);
if (proceedWithDownload) {
a.click();
window.URL.revokeObjectURL(downloadURL);
setTimeout(() => {
a.remove();
}, 4e4);
}
此脚本用于使用浏览器在移动设备上下载 .pkpass,但遇到错误。