Pastebin

Paste #1951: python file copy

< previous paste - next paste>

Pasted by tdn

Download View as text

First I create a temp file and then save the uploaded file there:


 42     tmpf = tempfile.NamedTemporaryFile('rw+b')
 43     # Save uploaded file to temp file
 44     log("Copying uploaded file to tempfile: %s" % tmpf.name)
 45     #shutil.copyfileobj(data.fp, tmpf)
 46     log("""img_data: 
 47     dir: %s
 48     type: %s
 49     """ % (dir(img_data), type(img_data)))
 50     tmpf.write(img_data.file.read()) # This is dangerous for big files

 .
 .
 .

Then I do some validation, like checking mime type and so on.

 .
 .
 .


then if all goes well, I copy the file and close the handles:

 93     # Copy file
 94     dst_path = os.path.join(img_path,img_filename)
 95     #print img_data
 96     log("Copying file to %s" % dst_path)
 97     fdst = open(dst_path, 'wb')
 98     #raw = open(tmpf.name).read()
 99     shutil.copyfileobj(open(tmpf.name), fdst)
100     #fdst.write(raw) # This is dangerous for big files
101     fdst.close()
102 
103     # Close (and thus delete) temp file
104     tmpf.close()

New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.