tencent cloud

All product documents
Tencent Cloud EdgeOne
Naming a Downloaded File
Last updated: 2023-12-13 10:55:46
Naming a Downloaded File
Last updated: 2023-12-13 10:55:46
In this example, the modification of the Content-Disposition in the response headers achieves the modification of the downloaded file name according to the fileName parameter in the request URL.
addEventListener('fetch', event => {
event.passThroughOnException();
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
const url = new URL(request.url);
const fileName = url.searchParams.get('fileName');

const response = await fetch(request);

// Evaluate the response status code and the search parameter
if (response.status !== 200 || !fileName) {
return response;
}

// Modify the Content-Disposition response header
response.headers.append('Content-Disposition', `attachment; filename="${fileName}"`);
return response;
}

Sample Preview

Enter a URL (such as https://example.com/origin.exe?fileName=modified.exe) that matches the trigger rule of edge function in the address bar of the browser on both the PC and mobile terminal to preview the example effect.




Related References

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon