0

I am trying to create a form and upload a file using Flask. The current code that I have saves the file successfully to my local server, but I do not have a way to find the upload status, if I upload a large file. I am planning to change the code to use Flask-Uploads, but the documentation does not mention anything about upload status which I could use to update my progress bar.

The entire app is made in Dash. The current code is as follows:

UPLOAD_FOLDER = os.get_cwd()
if request.method == 'POST':
    file = request.files['file']
    filename = file.filename
    file.save(os.path.join(UPLOAD_FOLDER, filename))

Is it at all possible to get file upload status using flask only? I cannot use JQuery solutions at the moment.

javapyscript
  • 720
  • 8
  • 22

1 Answers1

0

You can call the upload method using fetch and display a "please wait" message until the operation is done. You dont have to use JQuery.

Or

You can look here for a real progress bar solution.

balderman
  • 22,927
  • 7
  • 34
  • 52
  • Thanks. The second link was what I tried initially. The problem is that I am using Python code to save files, not js. And the script gets the save location from os.get_cwd(). I tried passing the result of os.get_cwd() to the JS upload path, but that raises a CORS issue. The entire mess is because the upload functionality is written in an IFrame embedded within a Dash application. This is to circumvent around the Dash bug that causes Chrome to crash if it tries to upload anything above 50Mb. Is it possible to get status of the upload once file.save is called in the script I wrote above? – javapyscript Oct 16 '19 at 11:00
  • Your "client" side is python as well as the server side? – balderman Oct 16 '19 at 11:06
  • Correct. The entire code is written in python in one file. Dash internally converts and separates this python code to a React frontend and a Flask backend, which is why I can use Flask code inside my dash app. This is its homepage: https://plot.ly/dash/ – javapyscript Oct 16 '19 at 11:09
  • I see... If you could use pure python I would suggest using requests. See https://stackoverflow.com/questions/13909900/progress-of-python-requests-post. This can be good replacement. Not sure how it is going to work with Dash. – balderman Oct 16 '19 at 11:13
  • Looks like you can inject js code into Dash. See https://dash.plot.ly/external-resources – balderman Oct 16 '19 at 11:16
  • Thanks, balderman. I did use that approach to implement the progress bar in JS/HTML. The js almost worked. My issue is that the upload location decided by python's os.get_cwd() is passed to JS, and it raises a CORS issue in Chrome. Chrome thinks its a local location, I am not sure why. That's when I started wondering if a pure python approach exists to get the file upload status. – javapyscript Oct 16 '19 at 11:28
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS - see how to bypass cors – balderman Oct 16 '19 at 11:32
  • Thanks, balderman. CORS issue was due to wrong input. but I got stuck somewhere else making it rather difficult to implement it. I have put it on hold for the time being. Thanks so much for being so helpful. – javapyscript Oct 16 '19 at 14:12