3

I am slightly new in REST framework. I am trying Restful Flask. The initial examples went fine but I want to upload files through Restful Flask. I tried some web based materials like, http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file http://blog.luisrei.com/articles/flaskrest.html Flask-RESTful - Upload image

But I am failing to work out a solution. If any one of the esteemed members may kindly suggest a solution, how may I tackle it?

I am using Python2.7.10 on Windows 7.

Community
  • 1
  • 1

1 Answers1

3

Well 2-step routine may needed to upload a file via flask.

1- Defining a route in your main.py:

@app.route('/file-upload', methods=['POST'])
@authorized
def file_upload(authorization_result=None):
    res = Utility()
    return res.file_upload(auth_result=authorization_result)

2- in our sample we have defined a Utility class with a method called file_upload. Let's see part of this method for file upload:

        def file_upload(self, auth_result):
            file_upload = request.files['file']
            if file_upload:
                filename = secure_filename(file_upload.filename)
                file_upload.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                app.logger.debug('File is saved as %s', filename)

Now you need to test your API. I have used POSTMAN here:

enter image description here

Alireza
  • 6,497
  • 13
  • 59
  • 132
  • Thanks. But which original tutorial you are referring to? I am trying to locate Utility class. If you may kindly give us the URL. – Richard Parker Jul 20 '15 at 14:22
  • This is derived from part of my own project. You can surf the net, there are tons of tuts out there. `Utility` class is declared to make life easier, you can remove it and move all codes into main route. – Alireza Jul 21 '15 at 05:27
  • @AlirezaHos can you please update answer with the details of testing the API via POSTMAN. – Rohit Srivastava Sep 16 '16 at 13:29