If we restart the server and upload a file to the server, the file will be uploaded to the created directory instead of the root directory. In the above code, we use the OS module of python to create a directory and saves every uploaded file to the created directory. # Saving the file in the required destinationį.save(os.path.join(app.config ,secure_filename(f.filename))) # this will secure the file We will see something as shown in the below image while running the above code.Īpp.config = upload_folder You can run the above code by just typing python app.py in the terminal. Next, we use python decorators to accept the post request for the file and save the file. Next, we use python decorators to define the routes for our Flask app and serve the HTML template that we created. After importing the required modules, we initialize the Flask app by calling the Flask() constructor. In the above code, we first imported the required modules to our code. Return 'file uploaded successfully' # Display thsi message after uploading ![]() If thod = 'POST': # check if the method is postį = request.files # get the file from the files objectį.save(secure_filename(f.filename)) # this will secure the file # The path for uploading the upload_file(): From flask import Flask, render_template, requestįrom werkzeug.utils import secure_filename
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |