Search code examples
pythonwindowsfile-uploadcherrypy

File upload issue - Python-WIndows-CheryyPy


Am a newbie for python and cherrypy. I am trying to upload file using following code:

@cherrypy.tools.noBodyProcess()
def POST(self,theFile=None):
    lcHDRS = {}
    for key, val in cherrypy.request.headers.iteritems():
        lcHDRS[key.lower()] = val
   formFields = myFieldStorage(fp=cherrypy.request.rfile,
                                headers=lcHDRS,
                                environ={'REQUEST_METHOD':'POST'},
                                keep_blank_values=True)

    dt = datetime.now()
    date = dt.strftime('%Y-%m-%d')
    dt = dt.strftime('%Y%m%d%H%M%S')
    theFile = formFields['theFile']
    theFile.filename = str(dt) + "file"
    shutil.copy2(theFile.file.name,os.path.join(absolutePath , theFile.filename))
    ...
    ...

I checked the path os.path.join(absolutePath , theFile.filename) and it is coming proper. The problem is that the code is working fine on Linux-ubuntu, but not on windows. Error invoked is: Edited

shutil.copy2(theFile.file.name,settings.UPLOAD_FILE_PATH + theFile.filename)
File "C:\Anaconda\lib\shutil.py", line 130, in copy2
copyfile(src, dst)
File "C:\Anaconda\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: 'c:\\users\\username\\appdata\\local\\temp\\tmpjy3gys'

Where am i going wrong? If you want any other information please let me know.


Solution

  • The issue may be related with some temporary file security which forbids reopening by a filename. Try replace shutil.copy2 call with:

    with open('/path/that/you/have/permission/to', 'wb') as f:
      shutil.copyfileobj(theFile.file, f)