Flask-restplus file download
However that method didn't work here. Here is the old code snippet:. I have assumed file is in current working directory. The path to download file can be adjusted accordingly. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Before we jump in and create any routes, I want to quickly discuss variable rules which we've touched on before.
Although not a necessity, Flask provides us with some useful converters to add an additional layer of validation to any values soming in via the URL. Using any of the converters listed above will convert the incoming variable into it's related type. Like many other important application configuration variables, we're going to add 3 new entries to our app. But before we do so, we're going to create some new directories and add some files for our users to download:. We're going to create 3 new entries in our app.
Now that we've updated our app config, let's go ahead and create our routes I'd recommend using a config file for this which you can read more about here. As we haven't set a variable rule, Flask will default to string and not allow any slashes. If you try a filename that doesn't exist, you'll get a Not Found error in your browser.
You'll notice these 2 routes are very similar to the first, with the addition of the filename variable. Create a project root directory called python-flask-rest-api-file-upload as per your chosen location. I will configure application through flask framework. You should not allow user to upload unlimited size of file due to security reasons or to avoid exhausting server space. I will show a successful message once file uploaded successfully or error message depending on the server error.
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コメント