如何在 Django 中从 MultiValueDict 读取或保存文件
How to read or save a file from MultiValueDict in Django
我正在从 Angular 向 Django 发送一个 excel 文件。我想使用 Pandas 读取文件并在文件中执行一些操作,但我不确定该怎么做。
class fileupload(APIView) :
def post(self, request):
f = request.FILES
print(f)
打印的时候显示如下,
<MultiValueDict: {'excelfile': [<InMemoryUploadedFile: New_Excel.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)>]}>
在这里,我想把这个文件保存到某个位置,然后使用pandas来执行操作,或者如果可能的话,直接需要使用pandas来读取文件。我是 Django 和 Pandas 的新手,所以如果有任何问题,请帮助..在此先感谢
class fileupload(APIView) :
def post(self, request):
f = request.data.getlist("excelfile")
print(f) # list of elements
现在循环f然后一个一个存储
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
file_objs = request.data.getlist('files')
for file_obj in file_objs:
path = default_storage.save(settings.MEDIA_ROOT, ContentFile(file_obj.read()))
print("images path are",path)
我正在从 Angular 向 Django 发送一个 excel 文件。我想使用 Pandas 读取文件并在文件中执行一些操作,但我不确定该怎么做。
class fileupload(APIView) :
def post(self, request):
f = request.FILES
print(f)
打印的时候显示如下,
<MultiValueDict: {'excelfile': [<InMemoryUploadedFile: New_Excel.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)>]}>
在这里,我想把这个文件保存到某个位置,然后使用pandas来执行操作,或者如果可能的话,直接需要使用pandas来读取文件。我是 Django 和 Pandas 的新手,所以如果有任何问题,请帮助..在此先感谢
class fileupload(APIView) :
def post(self, request):
f = request.data.getlist("excelfile")
print(f) # list of elements
现在循环f然后一个一个存储
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
file_objs = request.data.getlist('files')
for file_obj in file_objs:
path = default_storage.save(settings.MEDIA_ROOT, ContentFile(file_obj.read()))
print("images path are",path)