1

I have been trying to implement a simple Flask REST API which would accept an Excel file and store it into a directory.

Below is the snippet I am using

from flask import Flask, jsonify
from flask import abort
from flask import make_response
from flask import request
from werkzeug.utils import secure_filename
import os

app = Flask(__name__)

@app.route('/tasks/add', methods=['POST'])
def add_xlsx():
    file = request.files['ciq_file']
    if not file:
        return  jsonify({'error' : 'File is empty!'})

    fl_secure = secure_filename(file.filename)
    file.save('/data/prabir/t/testnew.xlsx', fl_secure)
    return jsonify({'status' : 'success'})

But, when I am trying to call this service, it is throwing TypeError: an integer is required

(venv) [xyx@xyz t]$ curl -F 'ciq_file=@/data/prabir/somefile.xlsx' http://localhost:5000/tasks/add

Curl command fails with the below error:

File "/data/prabir/t/app.py", line 35, in add_xlsx
    file.save('/data/prabir/t/testnew.xlsx', fl_secure)
  File "/data/prabir/venv/lib/python2.7/site-packages/werkzeug/datastructures.py", line 2803, in save
    copyfileobj(self.stream, dst, buffer_size)
  File "/data/TRpython/lib/python2.7/shutil.py", line 63, in copyfileobj
    buf = fsrc.read(length)
  File "/data/TRpython/lib/python2.7/tempfile.py", line 605, in read
    return self._file.read(*args)
TypeError: an integer is required

I am unable to figure out what could be the potential mistake I am doing. Any help would be appreciated.

1 Answer 1

1

The .save method takes up to 2 arguments: dst and buffer_size.

  • dst is either a string indicating the file path or a file object to write to.
  • buffer_size is an integer that specifies the buffer size to use when writing the file.

Right now you are passing two strings: '/data/prabir/t/testnew.xlsx' and fl_secure.

You probably want to do this:

file.save('/data/prabir/t/' + fl_secure)

This will save the file to the path /data/prabir/t/ with the original upload file name.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you James! I really feel stupid. I had gone through the save but it didn't occur to me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.