17

I can upload a File with flask by following Uploading Files:

  1. A <form> tag is marked with enctype=multipart/form-data and an <input type=file> is placed in that form.
  2. The application accesses the file from the files dictionary on the request object.
  3. Use the save() method of the file to save the file permanently somewhere on the filesystem.

But I don't know how to upload folder or some files. I searched, and I found Uploading multiple files with Flask.

However, still I don't know how to upload a folder and files that belong to the folder.

Could you please tell how?


Directory tree I am working on:

.
├── manage.py
├── templates
│   ├── file_upload.html
│   └── hello.html
└── uploads
    ├── BX6dKK7CUAAakzh.jpg
    └── sample.txt

Source code of uploading file:

from flask import Flask,abort,render_template,request,redirect,url_for
from werkzeug import secure_filename
import os
app = Flask(__name__)
UPLOAD_FOLDER = './uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 
@app.route('/')
def index():
    return redirect(url_for('hello'))

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name = None):
    return render_template('hello.html',name=name)

@app.route('/upload/',methods = ['GET','POST'])
def upload_file():
    if request.method =='POST':
        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
            return hello()
    return render_template('file_upload.html')


if __name__ == '__main__':
    app.run(debug = True)

template for file uploading(manage.py):

<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action='' method="POST" enctype="multipart/form-data">
    <p><input type='file' name='file[]' multiple=''>
    <input type='submit' value='upload'>
    </p>

</form>
jww
  • 97,681
  • 90
  • 411
  • 885
SamuraiT
  • 992
  • 3
  • 12
  • 31
  • 2
    Have you read [this question](http://stackoverflow.com/questions/5826286/how-do-i-use-google-chrome-11s-upload-folder-feature-in-my-own-code)? Remember, how the HTML form is displayed and how the user interacts with it are all dependent on the browser, who get their hints from HTML. Flask provides some abstraction over HTTP, but you still need to understand HTML to use it well. – Mark Hildreth Nov 13 '13 at 02:06
  • 1
    Thank you ! it does help ! Now, I understand: I can do it on Chrome but not others. – SamuraiT Nov 13 '13 at 03:11
  • if you answer as you commented, I can tag it as answer – SamuraiT Jan 05 '14 at 13:46
  • 1
    My comment was not actually an answer, but it seems to have brought you enough information to help you solve the problem yourself, correct? If so, you should write your own answer and accept it. – Mark Hildreth Jan 06 '14 at 01:13
  • Yes, it did help! I didn't know I can answer to my question. thank you! – SamuraiT Jan 06 '14 at 08:22
  • 1
    Please place answers in Answer blocks. Later, you can accept your own Answer. Also see [How does accepting an answer work?](https://meta.stackexchange.com/q/5234/173448) – jww Jun 06 '19 at 06:32

2 Answers2

9
file = request.files['file']

change it to

file = request.files['file[]']
Satyaki Sanyal
  • 1,201
  • 11
  • 12
6

the issue here is that flask's app.config isn't relative to itself, it's absolute. so when you put:

UPLOAD_FOLDER = './uploads' 

flask doesn't find this directory and returns a 500 error. if you changed it to:

UPLOAD_FOLDER = '/tmp'  

and then uploaded your file and navigated to the /tmp/ directory you would see it.

you will need to edit your path to the proper directory for the file to be uploaded properly.

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
GHETTO.CHiLD
  • 3,267
  • 1
  • 22
  • 34