Flask: How to handle application/octet-stream

前端 未结 2 1966
甜味超标
甜味超标 2021-02-08 13:06

I want to make a multiple file-upload form.I use jQuery File Uploader. My server-side code:

@app.route(\"/new/photogallery\",methods=[\"POST\"])
def newPhotoGall         


        
2条回答
  •  萌比男神i
    2021-02-08 13:26

    I was unable to get a request working using application/octet-stream type posts, but have used multipart/form-data type forms in the past to upload images using flask.

    I have extended what I have done in the past to support multiple upload files and this has worked leveraging werkzeug's FileStorage objects.

    The key here is setting up a post based route that is looking for a request element from a form. This should allow you to POST to the route either via a standard form or an AJAX call.

    Below is a simplified example that is using a form:

    Template for the view:

    
    
    
    
    jQuery File Upload Example
    
    
    {% if err %}
        

    {{ err }}

    {% endif %}
    {% if files %} {% for file in files %}

    Uploaded: {{ file }}

    {% endfor %} {% endif %}

    Flask App

    from flask import Flask, request, render_template
    from werkzeug import secure_filename, FileStorage
    import os
    
    # Flask functions
    app = Flask(__name__) 
    app.config.from_object(__name__)
    DEBUG = True 
    # add this so that flask doesn't swallow error messages
    app.config['PROPAGATE_EXCEPTIONS'] = True
    
    @app.route('/', methods=['GET', 'POST'])
    def uploader():
        if request.method =='POST' and request.files.getlist('files'):
            up_file_list = []
    
            # Iterate the through a list of files from the form input field
            for a_file in request.files.getlist('files'):
                if a_file.filename:
                    # Validate that what we have been supplied with is infact a file
                    if not isinstance(a_file, FileStorage):
                        raise TypeError("storage must be a werkzeug.FileStorage")
                    # Sanitise the filename
                    a_file_name = secure_filename(a_file.filename)
                    # Build target
                    a_file_target = os.path.join('/tmp/', a_file_name)
                    # Save file 
                    a_file.save(a_file_target)
                    up_file_list.append(a_file_name)
            # Return template
            if up_file_list:
                return render_template('uploader.html', err=None, files=up_file_list)
            else:
                return render_template('uploader.html', err='No Files Uploaded', files=None)
        else:
            return render_template('uploader.html', err=None, files=None) 
    
    
    # application execution 
    if __name__ == '__main__': 
        app.run()
    

提交回复
热议问题