我正在嘗試下載一個檔案,其中包含通過頁面上其他方式捕獲的資訊。在此示例中,這些方法已被替換為
var listContacts = 'Here is some text to download in a txt';以使代碼更簡單。它目前適用于 Windows 筆記本電腦和安卓手機。
這需要從本地存盤在手機上的 HTML 檔案脫機作業,而不能訪問服務器,因為這是使用它的方式。
我們測驗的 iPhone 運行的是 iOS 15,因此主導搜索結果的 iOS <13 下載錯誤不應該適用。Javascript 在 Safari 中啟用。它在 Chrome 中也不起作用(它在筆記本電腦和 Android 上起作用)。
我怎樣才能讓它在 iPhone 上作業?
function download(text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,'
encodeURIComponent(text));
pom.setAttribute('download', 'Contacts.txt');
pom.style.display = 'none';
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);
}
var listContacts = 'Here is some text to download in a txt';
<button onclick="download(listContacts)">Download</button>
uj5u.com熱心網友回復:
也許試試這個(對不起,在 Chrome iOS 中也不起作用,但在我的 Safari 中起作用):
https://jsfiddle.net/mplungjan/xu9ra6dL/
測驗鏈接:點擊
(function() {
var textFile = null,
makeTextFile = function(text) {
var data = new Blob([text], {
type: 'text/plain'
});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
document.getElementById('textbox').addEventListener('input', function() {
var link = document.getElementById('downloadlink');
link.href = makeTextFile(textbox.value);
link.style.display = 'block';
}, false);
})();
<textarea id="textbox">Type something here</textarea>
<a download="info.txt" id="downloadlink" style="display: none">Download</a>
uj5u.com熱心網友回復:
在這個問題上花了整整 3 天的時間,我現在知道由于沙盒和安全性,safari 不允許您運行本地存盤的 html 檔案。因此,如果不越獄我不打算做的老板昂貴的作業 iPhone,我的問題就無法解決。
如果 javascript 托管在 Internet 上,則它可以正常作業。它只是不會在本地作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/444831.html
標籤:javascript html css 苹果手机 下载
