Reputation: 683
I try to write simple RESTful API on Flask. Which way is good to let user upload a directory by using only web request without using a web browser? For instance:
curl ... http://localhost:5000/api/v1/uploaddirectory...
Or uploading directory in this case is possible only how transferring as an archive file?
curl -F "directory=@/home/directory.zip" "http://localhost:5000/api/v1/uploaddirectory"
Upvotes: 2
Views: 3674
Reputation: 755
As per the solution provided by @melchoir55 I was able to build a flask api which is able to read a single file at a time. Here, for my requirement I need to upload all files existing in a directory on to a server. What is the way to do so.
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('runDocumentManager',input_path=filename))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
This is the code for uploading file to the flask api server. Here I need to upload all files in a directory in a single go. How should I do it.
Upvotes: 0
Reputation: 7276
Getting flask to accept files is more code than I want to dump into a SO post. Since the problem is so general, I'm going to point you at resources which give you clear instructions on how to solve this problem.
Here is flask's boilerplate documentation on how to upload files: http://flask.pocoo.org/docs/0.12/patterns/fileuploads/
As it does for many problems, flask has its own module for file uploading. You could take a look at flask-uploads.
If you're really set on using curl
to upload a bunch of files in a directory, see this SO post for recursively uploading everything in a directory: Uploading all of files in my local directory with curl
Upvotes: 2