[英]Attribute error: 'str' object has no attribute 'read' python-django
So the scenario here is, I have a endpoint that will take a zipfile, unzip it and save it to media dir for now. 因此,这里的场景是,我有一个端点,该端点将提取一个zip文件,将其解压缩并暂时保存到媒体目录中。 Here's the whole code for that 这是整个代码
def get_filenames(path_for_zip):
with ZipFile(path_for_zip, 'r') as zip:
return zip.namelist()
class Upload(View):
def post(self, request):
context = {}
upload_file = request.FILES['document']
unzip_file = get_filenames(upload_file)
for files in unzip_file:
print(files)
fs = FileSystemStorage()
fs.save('read.jpg', files)
return render (request, 'toDo_app.html', context)
I am using FileSystemStorage
as you can see. 如您所见,我正在使用FileSystemStorage
。 ZipFile
is unzipping properly and i can see it in print(files)
but the issue is at FileSystemStorage
I guess, Its not getting saved and I get this error: ZipFile
正确解压缩,我可以在print(files)
看到它,但是我想问题出在FileSystemStorage
,它没有被保存,并且出现此错误:
attribute error 'str' object has no attribute 'read'.
Please point me out what did I do wrong and how should it be solved. 请指出我做错了什么以及应该如何解决。 Thank you. 谢谢。
@zeed namelist returns the name of the files in list ie string in the zip. @zeed namelist返回列表中文件的名称,即zip中的字符串。 While uploading read/open the file to buffer and upload 上载时读取/打开文件以进行缓冲和上载
for files in unzip_file:
print(files)
fs = FileSystemStorage()
with open(files, "rb") as outstream:
fs.save('read.jpg', outstream)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.