We have seen how to upload files using Flask, but sometimes we also want to serve a file to the user. First, create an HTML template in the templates folder, named the file download. Now, we need to create our app. In our app. Then, copy and paste the following code in the app. After copying the above code, run the code by typing the command python app. If everything works fine and the server starts without any problem, we have created our file downloader. If you click the download button present in the URL, then the file will start downloading.
For the above code to work properly, we need a file with the name sample. In this tutorial, we learned to create a file uploader and file downloader using the Flask library. In addition, we have seen how to configure the max size and extension of the file upload.
You may also want to see our step-by-step guide on creating a Flask app in python. How to upload a file with a python script? How to make it so you can acess it not only on the local network? Save my name, email, and website in this browser for the next time I comment.
August 27, August 7, July 31, July 17, May 28, November 14, July 20, December 2, June 23, This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More. Any idea? As mentioned in caniuse.
So if your links points to another domain, it hardly works anywhere for now. For it to work on Firefox, do document. You can also do link. Show 3 more comments. Tomer 1, 13 13 silver badges 14 14 bronze badges.
Chrome downloads, but Firefox just shows the picture. Saved me the time to test it only to find out it won't work. Latest Chrome Aug shows picture too because of an absurd security restriction so fail — user Chrome doesn't download for mp4 s — Nearoo. Show 2 more comments. Mark Amery k 65 65 gold badges silver badges bronze badges. Laura Chesches Laura Chesches 2, 17 17 silver badges 15 15 bronze badges.
Also the Edge 13 implementations is buggy because the name of the file is ignored and instead you get a file with an id as the name.
In my opinion, this is the correct answer to the question. The other answers make sense if you have to support older browsers and need a workaround. I dont' think is needed. It's already setting the 'src:url' in the creation step. I had to used "window. Maybe because I wanted to download 12 things at the same time?
Does not do anything in Chrome — jjxtra. This is base javascript function, so can be used in any platform where backend is in Javascript window. Rohit Parte Rohit Parte 2, 21 21 silver badges 17 17 bronze badges.
This is probably the easiest solution if you can set the contentType yourself. I use it as: window. If admin doesn't want to show the URL to the user than? Works on Chrome, Firefox and IE8 and above. Ismail Farooq 5, 1 1 gold badge 23 23 silver badges 43 43 bronze badges. EL missaoui habib EL missaoui habib 1, 1 1 gold badge 14 14 silver badges 24 24 bronze badges.
This also works if you don't append the link to the DOM. Unless headers returned from the server indicate to do otherwise, this will simply navigate to url , not download from it. Aaron Lelevier Aaron Lelevier Where is the download code? You're just adding an iframe — DevAB. This does not work if your filePath has a querystring since submitting a form will overwrite the querystring in the action attribute.
This also closes the websockets. This looks like a really complicated way to set window. Just window. Regardless of whether there's a downside to this solution, per se, you haven't provided any upside to using this over a link. And there is a downside: you can't use the download attribute this way to tell the browser that you want a download regardless of what headers the server returns, which you can do with an a element. I ended up using the below snippet and it works in most browsers, not tested in IE though.
Abk Abk 1, 1 1 gold badge 19 19 silver badges 28 28 bronze badges. What's the point of using MouseEvent here instead of always using click?
And why append the link to the document before clicking it? Maybe this has advantages over the simpler approach shown in stackoverflow. It's been a while I posted this answer. Mykhailo Seniutovych Mykhailo Seniutovych 2, 2 2 gold badges 24 24 silver badges 39 39 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Who is building clouds for the independent developer?
Exploding turkeys and how not to thaw your frozen bird: Top turkey questions Featured on Meta. Now live: A fully responsive profile. We should also check for file extension that the user is uploading as there would be some specific type of file that the site can process.
Also there is security issue if the user can upload any type of file. User might upload html file that can cause XSS cross site-scripting problems. For this post, we only need pdf file extensions as this Flask app would remove watermark from pdf files created by CamScanner App Famous App for scanning documents through Mobile Camera. Pdf files would have watermark at the bottom similar to what is shown in the image below :.
Function checks the filename for allowed file extension and if file type is supported the function returns True otherwise it returns False. We can add more extension types in allowed extensions set for supporting different type of file uploads.
0コメント