单纯h5移动端下载(IOS是无法直接下载文件的,已知的ppt、word、pdf会直接预览)
window.open();
var form = document.createElement('form');
form.action = url;
form.method = 'GET';
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
aDownlad(url, fileName) {
const link = document.createElement('a');
link.href = url;
link.target = '_blank';
link.download = fileName;
link.style.display = 'none';
document.body.append(link);
link.click();
document.body.removeChild(link);
}
iframeMethodOne(url) {
let myFrame = document.createElement('iframe');
myFrame.src = url;
myFrame.style.display = 'none';
document.body.appendChild(myFrame);
window.open(fileUrl);
}
iframeMethodTwo(url) {
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = "javascript: '<script>location.href=\"" + url + "\"<\/script>'";
document.getElementsByTagName('body')[0].appendChild(iframe);
}
复制剪切板兼容IOS
copyTxt (txt) {
var u = navigator.userAgent
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1
if (isAndroid) {
let _input = document.createElement('input')
_input.value = txt
document.body.appendChild(_input)
_input.select()
document.execCommand('Copy')
document.body.removeChild(_input)
if (document.execCommand('Copy')) {
} else {
}
} else {
const textString = txt.toString()
let input = document.querySelector('#copy-input')
if (!input) {
input = document.createElement('input')
input.id = 'copy-input'
input.readOnly = 'readOnly'
input.style.position = 'absolute'
input.style.left = '-1000px'
input.style.zIndex = '-1000'
document.body.appendChild(input)
}
input.value = textString
this.selectText(input, 0, textString.length)
console.log(document.execCommand('copy'), 'execCommand')
if (document.execCommand('copy')) {
document.execCommand('copy')
Toast('复制成功')
} else {
Toast('复制失败,请手动输入')
}
input.blur()
document.body.removeChild(input)
}
},
selectText (textbox, startIndex, stopIndex) {
if (textbox.createTextRange) {
const range = textbox.createTextRange()
range.collapse(true)
range.moveStart('character', startIndex)
range.moveEnd('character', stopIndex - startIndex)
range.select()
} else {
textbox.setSelectionRange(startIndex, stopIndex)
textbox.focus()
}
}