python - django如何上传文件夹

标签 python django file-upload upload uploading

我知道如何通过 django 上传多个文件,但是如果文件夹中有子文件夹,我在上传文件夹时会遇到问题。 django 无法接收子文件夹。我找到了原因,因为浏览器使用 '.'代表一个文件夹,但 Django 无法解析它然后停止解析。有没有一种优雅的方法来修复它?

python 代码:

def uploader_single(request):
    data = {}
    if request.method == 'POST':
        if True:
            for afile in request.FILES.getlist('file'):
                new_file = UploadFileSingle(file = afile)
                new_file.save()

            return HttpResponseRedirect('')
        else:
            print "form is not valid"
            return HttpResponseRedirect('')
    else:
        print 'not post'

Python代码:

class UploadFileSingle(models.Model):
    file        = models.FileField(upload_to='files/%Y/%m/%d', models.FilePath)
    uploaded_at = models.DateTimeField(auto_now_add=True)
    models.FilePathField.recursive = True
    models.FilePathField.allow_folders = True
    updated_at  = models.DateTimeField(auto_now=True)

    def some_folder = FilePathField(path='some_path', recursive=True, allow_files=True, allow_folders=True,)'

HTML代码:

<input type="file" name="file" multiple = "true" webkitdirectory="true" directory = "true"/>

最佳答案

有一个较新的主题问同样的问题,我试着回答:

Django directory upload get sub-directory names

基本上这是 Django 的默认行为,如果你想有不同的行为,你需要编写自己的上传处理程序

关于python - django如何上传文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21682692/

相关文章:

python - 矩形感兴趣区域超出范围时出错 - opencv

python - Django-tastypie : Any example on file upload in POST?

python - 在 Django 模型中使用 save() 会产生 TypeError

python - numpy 如何使用数组对数组进行切片索引?

python - 如何在 python 中使用 os.path.getsize() 和 * ?

jquery - blueimp jQuery 文件上传,无需 ajax

asp.net - 上传前设置文件路径和名称属性

javascript - 当我想创建多个输入类型时选择文件时出现问题 = 'file'

python - 如何修改元组列表中元组的每个元素

python - Django 查询集 If 语句永远不会计算为真?