zoukankan      html  css  js  c++  java
  • js下载流文件

    js代码:

    //  获取时间戳
    let timestamp = new Date().getTime();
    // 获取XMLHttpRequest
    let xmlResquest = new XMLHttpRequest();
    //  发起请求
    xmlResquest.open("GET", `${API}/export/exportCheckingIn?uuid=${params.name}&startTime=${params.startTime}&endTime=${params.endTime}`, true);
    // 设置请求头类型
    xmlResquest.setRequestHeader("Content-type", "application/json");
    //  设置请求token
    // xmlResquest.setRequestHeader(
     
    // );
    xmlResquest.responseType = "blob";
    //  返回
    xmlResquest.onload = function (oEvent) {
        let content = xmlResquest.response;
        // 组装a标签
        let elink = document.createElement("a");
        // 设置下载文件名
        elink.download = timestamp + ".xlsx";
        elink.style.display = "none";
        let blob = new Blob([content]);
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
    };
    
    xmlResquest.send();

    c#后台代码:

            public static HttpResponseMessage Download(string filePath)
            {
                string fullPath = BasePath + filePath;
    
                string fileName = Path.GetFileName(fullPath);
                FileStream fileStream = new FileStream(fullPath, FileMode.Open);
    
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(fileStream);
    
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
                MediaTypeHeaderValue contentType = response.Content.Headers.ContentType;
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = HttpUtility.UrlEncode(fileName)
                };
    
                response.Headers.Add("Access-Control-Expose-Headers", "FileName");
                response.Headers.Add("FileName", HttpUtility.UrlEncode(fileName));
    
                return response;
            }
  • 相关阅读:
    【应用】Linux内存调试工具:valgrind
    Python UnboundLocalError: local variable 'xxx' referenced before assignment 解决方法
    MYSQL连接时错误码2059解决办法
    Python encode()、decode()方法详解
    genymotion自动化使用
    论文数据集
    Native Apps、Web Apps和Hybrid Apps
    C# 中如何进行私有(private)函数测试
    Windows10 计划任务开始失败
    如果系统盘后面是恢复盘,如何扩充系统盘
  • 原文地址:https://www.cnblogs.com/zhuyapeng/p/13402697.html
Copyright © 2011-2022 走看看