In this article, we will be looking into the process of file uploading in Python using cgi environment. One often comes across various web applications in which the client or the users is required to upload data in the form of a file(eg. image file, an audio file, text file, etc). There are two aspects to uploading a file, as there are two sides to that particular interaction being client-side and the server-side. A form needs to be created to accept the user input in the form of a file.
An HTML form has different attributes that you can set it to, for instance, which URL is the uploaded data is to be submitted is done through the
action attribute. An enctype attribute called multi-part/form-data is required in a HTML form to upload a file. Secondaly we will be required to use the input tag of HTML and set it equal to “file”. This adds a upload button in addition to an input button in the form. The below code example demonstrates it well:
The output for the above HTML code would look like below:
In the above code, the attribute action has a python script that gets executed when a file is uploaded by the user. On the server end as the python script accepts the uploaded data the field storage object retrieves the submitted name of the file from the form’s “filename”. Now all the server needs to do it is read the file that has been uploaded and write it to the “fileitem”(say, ). At the end of this entire process the uploaded file now will be written to the server.
So, the python script looks somewhat like the below code:
Note: The above python script doesn’t work on every server as every server has its own dependencies to allow running a script in their server for security reasons. For instance if using an Azure server one would need to
import msvcrt which is Microsoft visual C++ runtime module to work.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Django - Upload files with FileSystemStorage
- Upload file and read its content in cherrypy python
- Associate user to its upload (post) in Django
- File upload Fields in Serializers - Django REST Framework
- How to Upload File using formidable module in Node.js ?
- How to Upload Project on GitHub from Pycharm?
- Downloading files from web using Python
- Working with csv files in Python
- Working with PDF files in Python
- Reading and Writing to text files in Python
- Listing out directories and files in Python
- Rename multiple files using Python
- Writing files in background in Python
- Python | Write multiple files data to master file
- Python | Reading .ini Configuration Files
- Python | Sort and store files with same extension
- Python | Move or Copy Files and Directories
- Python | Create Archives and Find Files by Name
- Python | Speech recognition on large audio files
- Python | How to use Multiple kv files in kivy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.