多个文件上传djang

2024-10-06 11:20:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我上传了多个文件到django,当我保存文件时,它只保存了一个。 执行示例:

------------------------------------ start files ------------------------
<MultiValueDict: {u'patientFiles': [<InMemoryUploadedFile: Registro VFC JOSE DE LA CRUZ_16_03_2015.txt (text/plain)>, <InMemoryUploadedFile: JDTD datos vfc_27_04_2015.txt (text/plain)>]}>
    *-1-*Registro VFC JOSE DE LA CRUZ_16_03_2015.txt
    *-2-*<django.core.files.storage.FileSystemStorage object at 0xaf731aac>
    *-1-*JDTD datos vfc_27_04_2015.txt
    *-2-*<django.core.files.storage.FileSystemStorage object at 0xaf731e4c>
    *-3-*JDTD datos vfc_27_04_2015.txt
    *-4-*JDTD%20datos%20vfc_27_04_2015.txt
    --------------------------------------------------------------------------

你可以看到它一次转到3点和4点,它不保存第一个文件。 你知道吗?在

谢谢,下一个是密码。在

这是django服务器代码:

^{pr2}$

这是HTML代码:

<form id="SuPF" class="form-group" enctype="multipart/form-data" action="/formSingupPatient" method="POST">
    <input type="file" multiple="true" name="patientFiles">
    <button type="submit" id="SubmitForm" class="btn btn-default">Submit</button>
</form>

Tags: 文件djangoformtxtdefileslaregistro
1条回答
网友
1楼 · 发布于 2024-10-06 11:20:23

最后我用python的filesapi保存了很多文件。我用以下内容更改startFiles代码:

def startFiles(self,request):
print colored("        - start files            -",'blue')
print colored(request.FILES,'blue')
for afile in request.FILES.getlist('patientFiles'):
    myfile = afile
    print colored("*-1-*" + str(myfile),'blue')
    fs = FileSystemStorage()
    print colored("*-2-*" + str(fs),'blue')
    filename = settings.MEDIA_ROOT + "/" + myfile.name
    f = open(filename,'w')
    print colored(f,'blue')
    f.write(myfile.read())
    f.close()
print colored("                                        ",'blue')

我用缓冲区创建一个文件并保存文件。在

在settings.MEDIA根目录是保存文件的系统路径。在

我希望对你将来有所帮助。在

相关问题 更多 >