views:

455

answers:

0

Hi Everyone,

I have recently been learning python and have run into a problem when uploading files larger than 1-2mb with cgi.

I am building a file uploader with a progress bar that updates with ajax. Everything is working great for smaller files and for larger files once it gets past the line:

form = cgi.FieldStorage()

I think I am having the same problem as this question but am having a really hard time figuring out how to implement the suggested solutions. Could someone show me an example to get started parsing the data myself with cgi.parse or the StringIO method? I was also looking into cgi.parse_multipart but couldn't figure it out. Below is what I have so far. Thanks in advance!

import cgi
import cgitb; cgitb.enable()
import os, stat
import sys

try:
    import msvcrt
    msvcrt.setmode(0, os.O_BINARY) #stdin = 0
    msvcrt.setmode(1, os.O_BINARY) #stdout = 1
except ImportError:
    pass

form = cgi.FieldStorage()

def save_uploaded_file(form_field, upload_dir):
    fileitem = form[form_field]
    final_size = get_final_file_size(fileitem.file)
    with open(os.path.join(upload_dir, fileitem.filename), 'wb') as fout:
        bytes_to_read = 500
        bytes_read = 0
        while bytes_read < final_size:
            bytes_read += bytes_to_read
            fout.write(fileitem.file.read(bytes_to_read))
            with open(os.path.join(upload_dir, os.path.splitext(fileitem.filename)[0] + "_progress.txt"), 'w') as fstats:
                fstats.write(get_percent_done(bytes_read, final_size))

def get_final_file_size(uploaded_file):
    uploaded_file.seek(0, 2)
    final_size = uploaded_file.tell()
    uploaded_file.seek(0)
    return final_size

def get_percent_done(bytes_read, final_size):
    percentage = (float(bytes_read) / final_size) * 100
    return str(percentage)

save_uploaded_file("filename", "D:\UploadFiles\Incoming")

print "Content-Type: text/html\n"