I do a lot of my projects in Python and one thing that I've faced several times was the classic file upload.
Here it is the smallest snippet of Python code I use for that:
input type="file" name="selectedfile"
input type="submit" name="Submit" value="Upload"
upload_dir = "c:/"
fileitem = request.file('selectedfile')
name=fileitem.filename[string.rfind(fileitem.filename,'\\'):]
fout = file(str(upload_dir+name), 'wb')
while 1:
chunk = fileitem.file.read(1000)
if not chunk: break
input type="submit" name="Submit" value="Upload"
upload_dir = "c:/"
fileitem = request.file('selectedfile')
name=fileitem.filename[string.rfind(fileitem.filename,'\\'):]
fout = file(str(upload_dir+name), 'wb')
while 1:
chunk = fileitem.file.read(1000)
if not chunk: break
fout.write (chunk)
fout.close()
fout.close()
Hope you will find it useful.
No comments:
Post a Comment