Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple File Upload and Server Side Validation #33

Open
skewwhiff opened this issue Dec 30, 2019 · 1 comment
Open

Multiple File Upload and Server Side Validation #33

skewwhiff opened this issue Dec 30, 2019 · 1 comment

Comments

@skewwhiff
Copy link

skewwhiff commented Dec 30, 2019

Hi @greyli ,
Awesome plugin. I am using it to upload multiple files along with some server-side validation. Is there a way to return the individual status of each uploaded file? It is clear how to do it when only one file is being uploaded from https://github.com/greyli/flask-dropzone/blob/master/docs/advanced.rst#server-side-validation

Here's a barebones implementation of what works so far. I currently just append all error messages with the corresponding filename:

:
: 
app.config['DROPZONE_UPLOAD_MULTIPLE'] = True
app.config['DROPZONE_MAX_FILE_SIZE'] = 10
app.config['DROPZONE_MAX_FILES'] = 5
app.config['DROPZONE_UPLOAD_ON_CLICK'] = True
: 
: 
@app.route('<upload_url>', methods=['GET', 'POST'])
def file_upload():
  if request.method=='POST':
    errors = []
    for key, f in request.files.items():
        if key.startswith('patent_file'):
            filename = secure_filename(f.filename)
            is_valid_file, msg, _, _ = validate_file(f, filename )
            if not is_valid_file:
               errors.append(filename + ' has error: '  + msg)
            else:
               f.save(os.path.join(app.config['UPLOADED_PATH'], patent_filename))
    if len(errors)>0:
      return ', '.join(errors), 400
  return render_template('index.html')

This works. But it shows the same error message across all files. I am looking for a way to send individual status message per file. Is this achievable through the plugin? Thanks in advance.

Other details:

$ pip show flask_dropzone
Name: Flask-Dropzone
Version: 1.5.4
Summary: Upload file in Flask with Dropzone.js.
Home-page: https://github.com/greyli/flask-dropzone
Requires: Flask
Required-by:
@yuxiaoy1
Copy link
Collaborator

yuxiaoy1 commented May 5, 2021

@skewwhiff The key is that you did not enable the parallel upload configuration, so there would be multiple requests for your multiple uploads, but you're handling the uploads in a way which there should only be one request for all the uploaded files.

And the solution is simple, handling the multiple file upload just the same as a single file upload, keep in mind that you should not enable the parallel upload specific to your need.

So the view function could be simplified something like below:

@app.route('<upload_url>', methods=['GET', 'POST'])
def file_upload():
    if request.method=='POST':
        f = request.files.get('patent_file')
        if f is None:
            return
        filename = secure_filename(f.filename)
        is_valid_file, msg, _, _ = validate_file(f, filename)
        if not is_valid_file:
            return f'{filename} has error: {msg}', 400
        f.save(os.path.join(app.config['UPLOADED_PATH'], patent_filename))
    return render_template('index.html')

Give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants