How to upload a file with Quasar's q-uploader component to a server running python/cgi?

99 Views Asked by At

Hello I am running Apache2 web server on Windows 10 and want to upload single file with Quasar's q-uploader component via python/cgi?

I tried a lot but cannot solve the problem alone.

There is an example for python/flask in Quasar'r web page but no example for python/cgi.

The following is my code which is not running:

#!/Python310/python.exe

import cgi, os
import cgitb; cgitb.enable(display=0, logdir="C:\\")

print("Content-Type: text/html")
print("Access-Control-Allow-Origin: *")
print("Access-Control-Allow-Methods: POST, GET, OPTIONS") 
print("Referrer-Policy: no-referrer")
  

form = cgi.FieldStorage()

try:
    fileitem = form['file']
except:
    print("An exception occurred")


# Test if the file was uploaded
if fileitem.filename:

    # strip leading path from file name
    # to avoid directory traversal attacks
    fn = os.path.basename(fileitem.filename)
    open('uploads/' + fn, 'wb').write(fileitem.file.read())
    message = 'The file "' + fn + '" was uploaded successfully'

else:
    message = 'No file was uploaded'

print(message)

I expected the code above uploads form Quasar front end but it did not.

0

There are 0 best solutions below