获取某个网址的文件内容

比如想在本地获取到https://unpkg.com/element-ui@2.15.1/lib/theme-chalk/index.css这个文件的内容

 1       getFile (url, isBlob = false) {
 2         return new Promise((resolve, reject) => {
 3           const client = new XMLHttpRequest()
 4           client.responseType = isBlob ? 'blob' : ''
 5           client.onreadystatechange = () => {
 6             if (client.readyState !== 4) {
 7               return
 8             }
 9             if (client.status === 200) {
10               const urlArr = client.responseURL.split('/')
11               resolve({
12                 data: client.response,
13                 url: urlArr[urlArr.length - 1]
14               })
15             } else {
16               reject(new Error(client.statusText))
17             }
18           }
19           client.open('GET', url)
20           client.send()
21         })
22       }
      getIndexStyle () {
        this.getFile('//unpkg.com/element-ui/lib/theme-chalk/index.css')
          .then(({ data }) => {
            this.originalStyle = this.getStyleTemplate(data)
          })
      },

 

posted @ 2021-05-06 13:56  芥末Yuki  阅读(218)  评论(0编辑  收藏  举报