4

I am trying to read a text file using flask framework. This is my code

import os
from flask import Flask, request, redirect, url_for, flash,jsonify
from werkzeug.utils import secure_filename


app = Flask(__name__)


@app.route('/index', methods=['GET'])
def index():
    return 'Welcome'

@app.route('/getfile', methods=['POST'])
def getfile():
    file = request.files['file']
    with open(file,'r') as f:
        file_content = f.read()
    return jsonify(file_content)    

if __name__ == '__main__':
    app.run(host = '0.0.0.0')

I am using POSTMAN to check it, pic

unfortunately it sends bad requests 400. I requested file as request.file. The .txt is residing in my disk at C:/Users/rbby/Desktop/Programs/hello.txt

The GET method is working fine. POST is not working for me. Is there anything else i should add from my above code?

I referred to this link Using Flask to load a txt file through the browser and access its data for processing Do i need to add these lines too? I don't understand these

filename = secure_filename(file.filename) 

# os.path.join is used so that paths work in every operating system
file.save(os.path.join("wherever","you","want",filename))
Jonreyan
  • 131
  • 2
  • 3
  • 14
  • what are you trying to is not clear, are you trying to send file for uploading or trying to send a file name using post method and return its content? – moghya Apr 01 '18 at 08:16
  • I want to read the content of a text file. I am sending a file name using POST method and want to return its content – Jonreyan Apr 01 '18 at 08:23

3 Answers3

7

Assuming that what you want POST /getfile to do is return the contents of the text file, then you can simply define your getfile() function as:

def getfile():
    file = request.files['file']
    return file.read()

Also make sure in Postman that you are setting the key of your file to file (as in the screenshot below):

Postman screenshot, with key set to file

EDIT: Note that the above method just takes a file and returns its contents - it does not take a filename, and return its contents.

Ollie
  • 1,641
  • 1
  • 13
  • 31
  • 1
    The OP want to pass the file name and get the file content back. You just return the content of uploaded file. – stamaimer Apr 01 '18 at 12:11
2

I think what you want is pass the file name to the server and get the file content which might be stored on the server file system.

You shouldn't get the file name from the file attribute of request. It store the file content you uploaded.

You should just pass the file name through the key-value form or json.

@app.route("/getfile", methods=["POST"])
def getfile():
    file_name = request.form["file_name"]
    with open(file_name, 'r') as f:
        file_content = f.read()
    return file_content
stamaimer
  • 6,227
  • 5
  • 34
  • 55
0

Below code will work. Have used Get here :

 from flask import Flask

app = Flask(__name__)

@app.route('/index', methods=['GET'])
def index():
    return 'Welcome'

@app.route("/getfile", methods=['GET'])
def getfile():
    with open("name of file", "r+") as f:
        data=f.read()
    return data



if __name__ == '__main__':
    app.run(host = '0.0.0.0')
user7258708
  • 251
  • 2
  • 7