【问题标题】:Can't fetch photo from url javascript无法从 url javascript 获取照片
【发布时间】:2021-05-26 00:46:28
【问题描述】:

我需要通过网址下载照片。我使用获取 API。我尝试了 cors 和 no-cors。

我想我一般不理解“http”中的某些内容(标题或其他内容)。

这是我的代码:

const options = {
   method: 'GET',
   mode: 'cors',
};

fetch('https://artlight.ru/upload/iblock/a47/ART-1013 size13.jpeg', options)
      .then((response: any) => {
         console.log(response);
         response.blob();
      })
      .then((blob: any) => {
         console.log(blob);
      })

无论我做什么,我都会得到相同的响应:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]:
   { body:
      PassThrough {
        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 6,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: true,
        allowHalfOpen: true,
        _transformState: [Object] },
     disturbed: false,
     error: null },
  [Symbol(Response internals)]:
   { url:
      'https://artlight.ru/upload/iblock/a47/ART-1013%20size13.jpeg',
     status: 200,
     statusText: 'OK',
     headers: Headers { [Symbol(map)]: [Object] },
     counter: 0 } }


undefined

我应该怎么做才能得到我的 blob?谢谢!

【问题讨论】:

    标签: node.js download fetch blob


    【解决方案1】:

    在此处发布的同一时刻找到了 axios。 这对我有用。

    path = Path.resolve(__dirname, '../../files', 'check.jpeg');
    
       async download(url: string) {
    
          const response = await Axios({
             method: 'GET',
             url: url,
             responseType: 'stream'
          })
    
          response.data.pipe(fs.createWriteStream(this.path));
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      相关资源
      最近更新 更多