Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

html - How do I download files from my Django website?

I have a small django website that allows file uploading hosted on pythonanywhere. The files are uploaded to aws s3.

Now the problem is that even with the presence of download attribute in the html, the browser still renders the file instead of downloading it

<a download href="{{file_url}}">Download</a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From Mozilla Developer Network Anchor element reference:

"....download only works for same-origin URLs, or the blob: and data: schemes..."

Your aws s3 links most probably are not same-origin (same domain etc) as your site.

If that is what you are running into, one work around that comes to my mind is, you can make on your site a transfer url that receives the document identifier, downloads from aws s3, and then forwards its content as response. This way you can also control content-type like headers that you may need to explicitly set to make the browser behave the way you want.

One addition, if you decide to have a solution like that, you have to take precautions to control the request content that transfer url, and only transfer content that your web site intended to. Otherwise you will have opened a vulnerability similar to what is called an "Open Redirect" vulnerability.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...