• .netcore vues实现https下载


     [HttpGet("ExportPolicy")]
            public FileStreamResult ExportPolicy(string id)
            {
                var data = service.ExportPolicy(id);
    
                Stream stream = new MemoryStream(data.Data);
    
                return File(stream, "application/x-compressed");
            }
    

      

     RequestModel<byte[]> ExportPolicy(string id);
    

      

    public RequestModel<byte[]> ExportPolicy(string id)
            {
                try
                {
                    RequestModel<byte[]> results = null;
                    var config = configService.GetConfig(id, ref results);
                    var data = baseService.GetGatekeeperResult<ExportPolicyModel>("", Urls.EXPORT_URL, config.MgtIp, config.Port);
    
                    if (data != null)
                    {
                        if (data.IsSuccess)
                        {
                            results.IsSuccess = data.Data.IsSuccess;
                            results.Data = GetFileByHttps(string.Format("https://{0}{1}", config.MgtIp, data.Data.Path));
                        }
                        else
                        {
                            results.IsSuccess = false;
                            results.ErrorMsg = data.ErrorMsg;
                        }
                    }
                    return results;
                }
                catch (System.Exception ex)
                {
                    loggerHelper.Error("网闸导出规则列表失败", ex);
                    throw ex;
                }
            }
    

      

    private byte[] GetFileByHttps(string url)
            {
                try
                {
                    byte[] bytes = null;
                    if (!string.IsNullOrEmpty(url))
                    {
                        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
                        WebClient webClient = new WebClient();
                        webClient.Headers.Add(HttpRequestHeader.UserAgent, "Other");
                        webClient.Headers.Add(HttpRequestHeader.Accept, "application/x-compressed");
                        bytes = webClient.DownloadData(url);
                    }
                    return bytes;
                }
                catch (Exception ex)
                {
                    loggerHelper.Error("网闸导出规则列表失败", ex);
                    throw ex;
                }
            }
    

      

    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true;
    

      vue:

    export function ExportPolicy(data) {
      return request({
        url: publicConst.prefixSoc + '/GatekeeperSynchronize/ExportPolicy',
        method: 'get',
        timeout: timeout,
        params: {
          id: data
        },
        responseType: 'blob'
      })
    }
    

      

    async exportFile() {
          this.$confirm('您确?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'info'
          }).then(() => {
            this.loading = true
            ExportPolicy(this.id).then(res => {
              if (res.size > 0 && res !== null) {
                var name = 'policy' + '.tgz'
                download(res, name)
                this.$message.success('数据导出成功')
                this.loading = false
              } else {
                this.$message.success('数据导出失败')
                this.loading = false
              }
            }).catch(error => {
              this.$message.error(error)
              this.loading = false
            })
          })
        },
    

      

    export function download(data, fileName) {
      if (!data) {
        return
      }
      const blob = new Blob([data], { type: 'application/x-compressed' })
      if ('download' in document.createElement('a')) {
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(url)
      } else { // IE 10+
        window.navigator.msSaveBlob(blob, fileName)
      }
    }
    

      

  • 相关阅读:
    [微信产品经理推荐] 有车一族福音,这个小程序能够帮到你很多忙,功能很逆天!
    微信小程序开闸,关于入口、推广、场景的一些观察与思考
    微信小程序体验(2):驴妈妈景区门票即买即游
    微信小程序的机会在于重新理解群组与二维码
    如何为你的微信小程序体积瘦身?
    体验报告:微信小程序在安卓机和苹果机上的区别
    微信小程序体验(1):携程酒店机票火车票
    张小龙宣布微信小程序1月9日发布,并回答了大家最关心的8个问题
    重点必看:小程序的服务范围限制有哪些?
    一些JS常用的方法
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/13865137.html
Copyright © 2020-2023  润新知