简体   繁体   English

Flask 'tuple' object 在保存文件时没有属性 'write'

[英]Flask 'tuple' object has no attribute 'write' on saving file

I am getting this wired error on flask when I want to save a file received on a post request.当我想保存在发布请求中收到的文件时,我在 flask 上收到此有线错误。 I tried debugging but I do not understand the error because I use flask save, and on google and other stack overflow questions I found that this has to to with python file API like missing open or wrong flags, but I do not have any flags, or do not need to open any file here.我尝试调试,但我不明白该错误,因为我使用 flask 保存,并且在 google 和其他堆栈溢出问题上,我发现这与 python 文件 API 相关,但没有打开或错误的标志,但没有任何标志,或者不需要在这里打开任何文件。

How I sent the file:我如何发送文件:

const uploadFile = async (file) =>{
  const formData = new FormData();
  formData.append("file", file);
  fetch("http://localhost:5000/files/upload", {method: "POST", body: formData});
}

How I recive the file:我如何接收文件:

@app.route('/files/upload', methods = ['POST'])
def recive_upload_file():
    file = request.files['file']
    filename = file.filename
    root_dir = os.path.dirname(os.getcwd())
    file.save((os.path.join(root_dir,'backend', 'upload'), filename))
    return "done"

As far as I can tell the file is sending correctly because if I try to print the filename in the recive_uploaded_file function I get the correct name.据我所知,文件发送正确,因为如果我尝试在 recive_uploaded_file function 中打印文件名,我会得到正确的名称。

The Error:错误:

Traceback (most recent call last):
  File "c:\Python37\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\Python37\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\Python37\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "c:\Python37\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\Python37\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\Python37\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\Python37\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "index.py", line 239, in upload_file
    file.save((os.path.join(root_dir,'backend', 'upload'), filename))
  File "c:\Python37\lib\site-packages\werkzeug\datastructures.py", line 3070, in save
    copyfileobj(self.stream, dst, buffer_size)
  File "c:\Python37\lib\shutil.py", line 82, in copyfileobj
    fdst.write(buf)
AttributeError: 'tuple' object has no attribute 'write'

I found the issue.我发现了这个问题。 I had the wrong path, the filename should be inside os.path.join()我的路径错误,文件名应该在 os.path.join()

file.save((os.path.join(root_dir,'backend', 'upload', filename)))

The file path you specified is incorrect.您指定的文件路径不正确。 You're trying to write to a tuple as file path:您正在尝试将元组作为文件路径写入:

>>> root_dir =  '/root/'
>>> filename = 'test.png'
>>> (os.path.join(root_dir,'backend', 'upload'), filename)
('/root/backend/upload', 'test.png')

You should move the filename inside the os.path.join call.您应该在os.path.join调用中移动文件名。

>>> os.path.join(root_dir,'backend', 'upload', filename)
'/root/backend/upload/test.png'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:'tuple'对象在写入文件时没有属性'write'错误 - AttributeError: 'tuple' object has no attribute 'write' error while writing into a file AttributeError:'tuple'对象没有属性'write' - AttributeError: 'tuple' object has no attribute 'write' AttributeError: 'tuple' object has no attribute 'write' 错误 - AttributeError: 'tuple' object has no attribute 'write' Error AttributeError:使用Flask SqlAlchemy,“ tuple”对象没有属性“ drivername” - AttributeError: 'tuple' object has no attribute 'drivername' using Flask SqlAlchemy AttributeError:'tuple'对象没有属性'strip':Flask Markdown? - AttributeError: 'tuple' object has no attribute 'strip' : Flask Markdown? Django表单保存问题AttributeError'tuple'对象没有属性'get' - Django form saving issue AttributeError 'tuple' object has no attribute 'get' AttributeError: 'tuple' object 没有属性 'write',实例分段 python - AttributeError: 'tuple' object has no attribute 'write' , instance segmentation python Python 错误:AttributeError: 'tuple' object 没有属性 'write' - Python error: AttributeError: 'tuple' object has no attribute 'write' 'tuple'对象没有属性'get' - 'tuple' object has no attribute 'get' 元组对象没有属性 'get' - tuple' object has no attribute 'get'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM