Socket
Socket
Sign inDemoInstall

m-downloader

Package Overview
Dependencies
3
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    m-downloader

A binary stream file download component based on native JS


Version published
Maintainers
1
Install size
4.88 MB
Created

Readme

Source
文件下载
  • 安装模块

    npm install m-downloader 或 yarn add m-downloader
    
  • 引用模块

    import m_downloader from 'm-downloader'
    
  • 通过代理拦截进行初始化配置

    const handler = {
      construct(target, [url, options] = args) {
        const method = options.method ? options.method.toUpperCase() : 'GET'
        if (method === 'GET') {
          Object.assign(options, { method, url })
        }
        if (method === 'POST') {
          Object.assign(options, {
            url,
            method,
            headers: {
              'Authorization': localStorage.getItem('AUTH_TOKEN')
            }
          })
        }
        return new target(options)
      }
    }
    const Downloader = new Proxy(m_downloader, handler)
    export default Downloader
    
  • GET请求方式

    const downloader = new Downloader('/api/download', {
      filename: 'Custom file name',
      getProgress(percentage) {
        console.log(`Download progress:${percentage}`)
      }
    })
    downloader
      .catch(error => { console.log(error) })
      .finally(() => console.log('A successful or failed operation, such as the handling of loading'))
    
  • POST请求方式

    const downloader = new Download('/api/download', {
      method: 'POST',
      data: { id: 5, type: 1 },
    })
    downloader
      .catch(error => { console.log(error) })
      .finally(() => console.log('A successful or failed operation, such as the handling of loading'))
    
  • 捕获服务端的JSON错误

    {
      "status": 500,
      "msg": "消息提示文案",
    }
    
    const downloader = new Download('/api/download', {
      method: 'POST',
      data: { id: 5, type: 1 },
    })
    downloader
      .catch(error => {
        console.log('errorMsg', error.statusText)
      })
    

Keywords

FAQs

Last updated on 09 Nov 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc