Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large file upload in Flask

I am attempting to implement a flask application for uploading files. This file could be very large. For example, almost 2G in size.

I have finished the server side process function like this:

@app.route("/upload/<filename>", methods=["POST", "PUT"])
def upload_process(filename):
    filename = secure_filename(filename)
    fileFullPath = os.path.join(application.config['UPLOAD_FOLDER'], filename)
    with open(fileFullPath, "wb") as f:
        chunk_size = 4096
        while True:
            chunk = flask.request.stream.read(chunk_size)
            if len(chunk) == 0:
                return

            f.write(chunk)
    return jsonify({'filename': filename})

As for browser side, I should give users a from to submit the file. One file at a time. Show progressbar to indicate the uploading process. But I have no idea about the browser side code. How can I use javascript code to start the uploading and show it status?

like image 500
Terry.Su Avatar asked Jun 27 '16 07:06

Terry.Su


People also ask

Does flask use werkzeug?

Flask wraps Werkzeug, using it to handle the details of WSGI while providing more structure and patterns for defining powerful applications. Werkzeug includes: An interactive debugger that allows inspecting stack traces and source code in the browser with an interactive interpreter for any frame in the stack.

What is Secure_filename in Python?

the secure_filename docstring also points out some other functionality: Pass it a filename and it will return a secure version of it. This filename can then safely be stored on a regular file system and passed to :func: os.


1 Answers

This will be a difficult task for your to figure out on your own. I would suggest a plugin like https://blueimp.github.io/jQuery-File-Upload/

You can see from this projects source code they use a method name which essentially looks at how large the file is and how much data has been transferred thus far and how much remains to show a percentage complete div.

code example from this project

progressall: function (e, data) {
                var $this = $(this);
                $this.find('.fileupload-progress')
                    .find('.progress').progressbar(
                        'option',
                        'value',
                        parseInt(data.loaded / data.total * 100, 10)
                    ).end()
                    .find('.progress-extended').each(function () {
                        $(this).html(
                            ($this.data('blueimp-fileupload') ||
                                    $this.data('fileupload'))
                                ._renderExtendedProgress(data)
                        );
                    });
            }

https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload-jquery-ui.js

So if you do want to come up with your own solution, I would suggest you start by building a UI div rectangle which has a dynamic width which updates according to your percentage calculation based upon the file upload size and data uploaded... or just go with an already established solution.

like image 176
Chris Hawkes Avatar answered Sep 29 '22 21:09

Chris Hawkes