Open In App

Read File Without Saving in Flask

Improve
Improve
Like Article
Like
Save
Share
Report

Flask is a flexible, lightweight web-development framework built using python. A Flask application is a Python script that runs on a web server, which listens to HTTP requests and returns responses. It is designed for simple and faster development. In this article we will discuss how can we Read files without Saving them in Flask, we will also verify that the file uploaded successfully or not.

Required Module

Install Flask by running the pip command in your terminal.

pip install Flask

Implementation to Upload and Read the CSV File in Flask Application

Here in this Flask application, we are creating a folder named ‘Read_File’ to read our files. Create a Virtual environment, After making the virtual environment we will create one templates folder in which we will write our HTML part and we also create the app.py file in which we will write our main part of the code in Flask Python.

File Structure:

Read file without Saving in Flask

 

index.html

In this example, we will operate the operation of the read file without saving it in the Flask.

HTML




<!DOCTYPE html>
<html>
    <head>
        <title>Read File Without Saving in Flask</title>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="file">
            <input type="submit" value="Submit">
        </form>
    </body>
</html>


Output:

Read file without Saving in Flask

 

app.py 

In this example, the Flask application has an endpoint at the root URL /. The endpoint can be accessed via the GET and POST methods. When a user submits the form with an image file, the file is uploaded to the server via the POST method, and its content is read using the request.files.get(“file”) method. The content of the file is then encoded as a base64 string and included as the src attribute of an HTML img tag. This allows the image to be displayed in the browser without saving it to the server. When the endpoint is accessed via the GET method, the index.html template is returned, which contains the form for uploading the file.

Python




from flask import Flask, request, render_template
  
app = Flask(__name__)
  
UPLOAD_FOLDER = 'static/uploads/'
  
@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        file = request.files.get("file")
        file_content = file.read()
          
        # check if file loaded successfully or not
        if file_content:
            return "Uploaded Successful"
        else:
            return "Uploaded Unsuccessful"
  
    return render_template("index.html")
  
if __name__ == "__main__":
    app.run(debug=True)


Output:

Read file without Saving in Flask

 

Read file without Saving in Flask\

When no file is uploaded 

Read file without Saving in Flask

When a file is uploaded 



Last Updated : 21 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads