<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="download()">下载</button>
<input type="file" value="文件预览" id="upload">
<script>
// ---------------------------------------------------------------------------------------------
// 文件下载 并保存
// "http://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + 440300 + "_full"
function ajax({
url
}) {
// 参考 https://www.w3school.com.cn/xml/xml_http.asp
let xhr = new XMLHttpRequest()
xhr.open("GET", url, true); // 也可以使用POST方式,根据接口 true 默认 异步 false 同步
xhr.responseType = "blob"; // 返回类型blob
// xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xhr.send()
xhr.onload = function () {
// 请求完成
if (this.status === 200) {
// console.log(this.response, '====') // 返回数据
let debug = this.response
// -------------------------------文件下载
// var reader = new FileReader();
// reader.readAsDataURL(debug); // 转换为base64,可以直接放入a表情href
// reader.onload = function (e) {
// // 转换完成,创建一个a标签用于下载
// var a = document.createElement("a");
// a.download = "000.json";
// a.href = e.target.result;
// a.click()
// }
// blob 下载
var a = document.createElement('a')
var blob = new Blob([debug], { type: 'text/plain' })
var url = window.URL.createObjectURL(blob)
a.href = url
a.download = "000.json"
a.click()
window.URL.revokeObjectURL(url)
}
}
}
// ajax({ url: "http://geo.datav.aliyun.com/areas_v3/bound/geojson?code=440300_full" })
// ---------------------------------------------------------------------------------------------
// 文件操作
function download() {
var data1 = "a";
var blob1 = new Blob([data1]);
console.log(blob1); //输出:Blob {size: 1, type: ""}
var debug = { hello: "world" };
var blob = new Blob([JSON.stringify(debug, null, 2)], { type: 'application/json' });
// var file = new File([mobileCode], "手机号.txt", { type: "text/plain;charset=utf-8" });
console.log(blob) // 输出 Blob(22) {size: 22, type: "application/json"}
const a = document.createElement('a');
document.body.appendChild(a)
a.style.display = 'none'
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = 'xx.json';
a.click();
document.body.removeChild(a)
window.URL.revokeObjectURL(url);
}
// 文件
// 文件操作
function file() {
var reader = new FileReader();
reader.readAsDataURL(blob); // 转换为base64,可以直接放入a表情href
reader.onload = function (e) {
// 转换完成,创建一个a标签用于下载
var a = document.createElement("a");
a.download = name + ".xls";
a.href = e.target.result;
document.append(a); // 修复firefox中无法触发click
a.click();
document.body.removeChild(a)
}
}
const input = document.querySelector("#upload");
input.addEventListener('change', function () {
const files = this.files;
console.log(files, '===123')
const fileList = Object.entries(files).map(([_, file]) => {
// 导入浏览器重
// 文件的方法
// var file = fileInput.files[0];
// if (window.FileReader) {
// var reader = new FileReader();
// reader.onloadend = function (evt) {
// if (evt.target.readyState == FileReader.DONE) {
// callback(evt.target.result);
// }
// };
// // 包含中文内容用gbk编码
// reader.readAsText(file, 'gbk');
// }
// ------------------------------------------------------------------
const reader = new FileReader(); //这是核心,读取操作就是由它完成.
reader.readAsText(file);//读取文件的内容,也可以读取文件的URL
reader.onload = function () {
//当读取完成后回调这个函数,然后此时文件的内容存储到了result中,直接操作即可
console.log(this.result);
let debug = this.result
// ======================== 打印数据
// document.write(debug)
// ======================== 标签展示
// // 文件预览
// let textarea = document.createElement('textarea')
// textarea.value = debug;
// document.body.appendChild(textarea)
// ======================== 打开窗口
// var x = window.open()
// x.document.write(`<pre>${debug}</pre>`)
// x.document.close()
// ======================== 打开窗口
// // 生成文件链接 iframe 来解析文件连接 ======================================
// var blob = new Blob([debug], { type: 'text/plain' })
// var url = window.URL.createObjectURL(blob)
// // charset="gb2312"
// // charset="gbk"
// // charset="UTF-8"
// // data:text/javascript;charset=UTF-8;base64,Y2hhcnNldCA9ICJnYjIzMTIiOw0KdmFyIGEgPSAic3NzcyI7DQp2YXIgYiA9ICLmiJHmmK/osIEiOw0K
// // 打开预览 浏览器默认支持的格式都可以预览,如:txt,pdf,图片
// var iframe = `<iframe width='100%' height='100%' src=${url}></iframe > `
// var x = window.open()
// x.document.open()
// x.document.write(iframe)
// x.document.close()
}
// ========================================================================== base64 预览
// const reader = new FileReader(); //这是核心, 读取操作就是由它完成.
// reader.readAsDataURL(file,); // 生成base64 图片 内容 字节码 链接
// reader.onload = (e) => {
// file.preview = e.target.result;
// // 转化样式
// // // 打开预览 浏览器默认支持的格式都可以预览,如:txt,pdf,图片 (网页 UTF-8 iframe 是gdk 或者其他的 因此存在编码格式不一致 )
// // data:text/javascript;charset=UTF-8;base64,Y2hhcnNldCA9ICJnYjIzMTIiOw0KdmFyIGEgPSAic3NzcyI7DQp2YXIgYiA9ICLmiJHmmK/osIEiOw0K
// var iframe = "<iframe width='100%' height='100%' src='" + e.target.result + "'></iframe>"
// // 引入
// // document.body.getElementsByTagName('button')[0].innerHTML = iframe
// var x = window.open()
// x.document.open()
// x.document.write(iframe)
// x.document.close()
// // 打开界面方式
// // <a href="data:xxxxx"> 点击跳转
// // window.open(“data:xxx”)
// // window.location="data:xxx"
// // 打开图片
// // const img = new Image();
// // img.src = this.base64String;
// // const newWin = window.open("", "_blank");
// // newWin.document.write(img.outerHTML);
// // newWin.document.title = "流程图"
// // newWin.document.close();
// // }
return file;
});
/*
* file中的preview存的就是可以预览使用url,如果你需要保证fileList的顺序,
* 请不要使用这种方式,你可以采用索引的方式存储,来保证它的顺序一致性
*/
// console.log(fileList);
}, false);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="download()">下载</button>
<input type="file" value="文件预览" id="upload">
<button id="ajax">ajax</button>
<script src="./message.js"></script>
<script>
// console.log(city, '======')
// ---------------------------------------------------------------------------------------------
// 文件下载 并保存
// "http://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + 440300 + "_full"
function ajax({
url,
name
}) {
// 参考 https://www.w3school.com.cn/xml/xml_http.asp
let xhr = new XMLHttpRequest()
xhr.open("GET", url, true); // 也可以使用POST方式,根据接口 true 默认 异步 false 同步
xhr.responseType = "blob"; // 返回类型blob
// xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xhr.send()
xhr.onload = function () {
// 请求完成
if (this.status === 200) {
// console.log(this.response, '====') // 返回数据
let debug = this.response
// -------------------------------文件下载
// var reader = new FileReader();
// reader.readAsDataURL(debug); // 转换为base64,可以直接放入a表情href
// reader.onload = function (e) {
// // 转换完成,创建一个a标签用于下载
// var a = document.createElement("a");
// a.download = "000.json";
// a.href = e.target.result;
// a.click()
// }
// blob 下载
var a = document.createElement('a')
var blob = new Blob([debug], { type: 'text/plain' })
var url = window.URL.createObjectURL(blob)
a.href = url
a.download = name + ".json"
a.click()
window.URL.revokeObjectURL(url)
}
}
}
document.getElementById('ajax').onclick = function () {
var arr = Object.keys(city)
arr.forEach((item, index) => {
ajax({ url: "http://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + item + "_full", name: item })
});
}
// ---------------------------------------------------------------------------------------------
// 文件操作
function download() {
var data1 = "a";
var blob1 = new Blob([data1]);
console.log(blob1); //输出:Blob {size: 1, type: ""}
var debug = { hello: "world" };
var blob = new Blob([JSON.stringify(debug, null, 2)], { type: 'application/json' });
// var file = new File([mobileCode], "手机号.txt", { type: "text/plain;charset=utf-8" });
console.log(blob) // 输出 Blob(22) {size: 22, type: "application/json"}
const a = document.createElement('a');
document.body.appendChild(a)
a.style.display = 'none'
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = 'xx.json';
a.click();
document.body.removeChild(a)
window.URL.revokeObjectURL(url);
}
// 文件操作
function file() {
var reader = new FileReader();
reader.readAsDataURL(blob); // 转换为base64,可以直接放入a表情href
reader.onload = function (e) {
// 转换完成,创建一个a标签用于下载
var a = document.createElement("a");
a.download = name + ".xls";
a.href = e.target.result;
document.append(a); // 修复firefox中无法触发click
a.click();
document.body.removeChild(a)
}
}
const input = document.querySelector("#upload");
input.addEventListener('change', function () {
const files = this.files;
console.log(files, '===123')
const fileList = Object.entries(files).map(([_, file]) => {
// 导入浏览器重
// 文件的方法
// var file = fileInput.files[0];
// if (window.FileReader) {
// var reader = new FileReader();
// reader.onloadend = function (evt) {
// if (evt.target.readyState == FileReader.DONE) {
// callback(evt.target.result);
// }
// };
// // 包含中文内容用gbk编码
// reader.readAsText(file, 'gbk');
// }
// ------------------------------------------------------------------
const reader = new FileReader(); //这是核心,读取操作就是由它完成.
reader.readAsText(file);//读取文件的内容,也可以读取文件的URL
reader.onload = function () {
//当读取完成后回调这个函数,然后此时文件的内容存储到了result中,直接操作即可
console.log(this.result);
let debug = this.result
// ======================== 打印数据
// document.write(debug)
// ======================== 标签展示
// // 文件预览
// let textarea = document.createElement('textarea')
// textarea.value = debug;
// document.body.appendChild(textarea)
// ======================== 打开窗口
// var x = window.open()
// x.document.write(`<pre>${debug}</pre>`)
// x.document.close()
// ======================== 打开窗口
// // 生成文件链接 iframe 来解析文件连接 ======================================
// var blob = new Blob([debug], { type: 'text/plain' })
// var url = window.URL.createObjectURL(blob)
// // charset="gb2312"
// // charset="gbk"
// // charset="UTF-8"
// // data:text/javascript;charset=UTF-8;base64,Y2hhcnNldCA9ICJnYjIzMTIiOw0KdmFyIGEgPSAic3NzcyI7DQp2YXIgYiA9ICLmiJHmmK/osIEiOw0K
// // 打开预览 浏览器默认支持的格式都可以预览,如:txt,pdf,图片
// var iframe = `<iframe width='100%' height='100%' src=${url}></iframe > `
// var x = window.open()
// x.document.open()
// x.document.write(iframe)
// x.document.close()
}
// ========================================================================== base64 预览
// const reader = new FileReader(); //这是核心, 读取操作就是由它完成.
// reader.readAsDataURL(file,); // 生成base64 图片 内容 字节码 链接
// reader.onload = (e) => {
// file.preview = e.target.result;
// // 转化样式
// // // 打开预览 浏览器默认支持的格式都可以预览,如:txt,pdf,图片 (网页 UTF-8 iframe 是gdk 或者其他的 因此存在编码格式不一致 )
// // data:text/javascript;charset=UTF-8;base64,Y2hhcnNldCA9ICJnYjIzMTIiOw0KdmFyIGEgPSAic3NzcyI7DQp2YXIgYiA9ICLmiJHmmK/osIEiOw0K
// var iframe = "<iframe width='100%' height='100%' src='" + e.target.result + "'></iframe>"
// // 引入
// // document.body.getElementsByTagName('button')[0].innerHTML = iframe
// var x = window.open()
// x.document.open()
// x.document.write(iframe)
// x.document.close()
// // 打开界面方式
// // <a href="data:xxxxx"> 点击跳转
// // window.open(“data:xxx”)
// // window.location="data:xxx"
// // 打开图片
// // const img = new Image();
// // img.src = this.base64String;
// // const newWin = window.open("", "_blank");
// // newWin.document.write(img.outerHTML);
// // newWin.document.title = "流程图"
// // newWin.document.close();
// // }
return file;
});
/*
* file中的preview存的就是可以预览使用url,如果你需要保证fileList的顺序,
* 请不要使用这种方式,你可以采用索引的方式存储,来保证它的顺序一致性
*/
// console.log(fileList);
}, false);
</script>
</body>
</html>