下载文件downFile

下载文件downFile

定义下载文件的方法downFile:

import axios from 'axios'
import qs from 'qs'

/**
 * 下载文件
 * @param url
 * @param fileName
 * @param params
 */

export function downFile (url, fileName, params, method) {
  const downLink = document.createElement('a')
  downLink.download = fileName
  document.body.append(downLink)

  if (!method) {
    method = 'post'
  }

  return new Promise((resolve, reject) => {
    axios({
      method,
      url,
      responseType: 'arraybuffer',
      data: qs.stringify(params)
    }).then(res => {
      console.log(res)
      // workaround
      if (res.data.byteLength < 100) {
        reject('创建失败')
      }
      if (res.status === 200) {
        const blob = new Blob([res.data])
        downLink.href = URL.createObjectURL(blob)
        downLink.click()
        document.body.removeChild(downLink)
        resolve()
      } else {
        reject(res.statusText)
      }
    }).catch(error => {
      document.body.removeChild(downLink)
      reject(error)
    })
  })
}

导入下载文件的方法downFile并使用:

import { downFile } form "./downFile.js"

downFile("下载地址url","下载文件名称"+".doc",params)
 .then()
 .catch(error=>{
    console.log("downFile error: ",error);
 });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单的网络下载文件程序。 WSADATA wdata; WSAStartup(MAKEWORD(2,2),&wdata); int sockfd; char buffer[1024]; struct sockaddr_in server_addr; struct hostent *host; int portnumber,nbytes; char host_addr[256]; char host_file[1024]; char local_file[256]; FILE * fp; char request[1024]; int isend, totalsend; int i; char * pt; memset(host_file,0,sizeof(host_file)); memcpy(host_file,"down/HB_EHSniffer36_ljh.rar",strlen("down/HB_EHSniffer36_ljh.rar")+1); if((host=gethostbyname("nmas.onlinedown.net"))==NULL)/*取得主机IP地址*/ { fprintf(stderr,"Gethostname error, %s\n", strerror(errno)); exit(1); } /* 客户程式开始建立 sockfd描述符 */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)/*建立SOCKET连接*/ { fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); exit(1); } /* 客户程式填充服务端的资料 */ memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(80); server_addr.sin_addr=*((struct in_addr *)host->h_addr); /* 客户程式发起连接请求 */ if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)/*连接网站*/ { fprintf(stderr,"Connect Error:%s\a\n",strerror(errno)); exit(1); } memset(request,0,1024); sprintf(request,"GET /%s HTTP/1.1\r\nAccept: */*\r\nCache-Control: no-cache\r\nConnection: close\r\nHost: %s\r\nPragma: no-cache\r\nReferer: http://www.onlinedown.net/soft/15450.htm\r\nRange: bytes=0-\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; )\r\n\r\n" ,host_file,"nmas.onlinedown.net"); printf("%s", request);/*准备request,将要发送给主机*/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值