os.path.join(settings.MEDIA_ROOT, uploaded_filelist.name) 提示没有'list' object has no attribute 'name'
时间: 2023-12-14 14:02:56 浏览: 30
抱歉,我之前给出的代码中有一个错误。如果您得到了"list object has no attribute 'name'"的错误提示,那么很可能是因为`uploaded_filelist`是一个列表对象,而不是单个文件对象。请使用`uploaded_filelist`列表中的每个文件对象进行保存。以下是修正后的代码示例:
```python
from django.shortcuts import render
from django.conf import settings
def upload(request):
if request.method == 'POST':
uploaded_files = request.FILES.getlist('file') # 获取上传的文件列表
for uploaded_file in uploaded_files:
# 构建目标文件路径
file_path = os.path.join(settings.MEDIA_ROOT, uploaded_file.name)
# 保存文件到服务器
with open(file_path, 'wb') as file:
for chunk in uploaded_file.chunks():
file.write(chunk)
# 返回成功页面或其他操作
return render(request, 'upload_success.html')
return render(request, 'upload.html')
```
在这个修正后的代码示例中,我们使用`request.FILES.getlist('file')`来获取上传的文件列表。然后,我们遍历每个文件对象,并按照之前的方式保存文件到服务器。
希望这次的解释能够帮助到您!如果您还有其他问题,请随时提问。