文件下载 上传 编辑 预览 web端


<!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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web修理工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值