简体   繁体   中英

Cloudflare Worker - Download a File using Fetch

I making a cloudflare worker that have to fetch a url pointing to a file. I want to redirect the request to download this file. The download happens, but with no extension and no name of the file. Can i read the response (return binary/octet-stream) and set the filename before download the file? Or some way to read the binary response and build a file with a name and download the file?

Im using fetch:

let file_response = await fetch(url)
.then((data) => {

}

thanks for the help!

After a while, i found a way. Maybe can help others!

var requestOptions = {
      method: 'GET',
      redirect: 'follow'
    };

    let file_response = await fetch(url,requestOptions)
    let data = await file_response.blob();
    contentDisposition = "attachment; filename=" + "file123.zip";
    return new Response(data, {
      status: 200,
      headers: { "content-type": "application/octet-stream", "Content-Disposition": contentDisposition}
    })

and now, the file is downloaded with the given name "file123.zip"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM