如何使用django-filebrowser在上传文件到文件夹之前重命名文件

时间:2021-12-01 02:53:39

like django's upload_to

django的upload_to

def upload_to(instance, filename):
    filename = time.strftime('%Y%m%d%H%M%S')
    ym = time.strftime('%Y%m')
    return 'uploads/%s/%s.jpg' % (ym,filename)

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    photo = models.ImageField(u"Image (Upload)",upload_to=upload_to)

file saved 'uploads/%s/%s.jpg'

文件保存的上传/ % s / % s.jpg

but change for

但改变

photo = FileBrowseField("Image", max_length=200, directory="uploads/", extensions=[".jpg"], blank=True, null=True)

How to rename a file before uploading it to a folder like django's upload_to

如何在将文件上传到django的upload_to这样的文件夹之前重命名文件

1 个解决方案

#1


1  

In filebrowser/sites.py you can create a hook for this when uploading / handling uploads:

在filebrowser /网站。py上传/处理上传时可以为此创建一个挂钩:

def _upload_file(self, request):
    """
    Upload file to the server.
    """
    if request.method == "POST":
        folder = request.GET.get('folder', '')

        if len(request.FILES) == 0:
            return HttpResponseBadRequest('Invalid request! No files included.')
        if len(request.FILES) > 1:
            return HttpResponseBadRequest('Invalid request! Multiple files included.')

        filedata = list(request.FILES.values())[0]

        fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
        folder = fb_uploadurl_re.sub('', folder)

        path = os.path.join(self.directory, folder)
        # we convert the filename before uploading in order
        # to check for existing files/folders
        file_name = convert_filename(filedata.name)
        filedata.name = file_name
        file_path = os.path.join(path, file_name
        ....

You can modify file_path here to whatever you like, or modify the file name.

您可以在这里修改file_path到任何您喜欢的地方,或者修改文件名。

For those of you that just want to ensure that files are not overwritten, you can set the FILEBROWSER_OVERWRITE_EXISTING flag in your settings.py as such:

对于那些只想确保文件不被覆盖的人,可以在设置中设置FILEBROWSER_OVERWRITE_EXISTING标记。py是这样的:

FILEBROWSER_OVERWRITE_EXISTING = False

This will ensure that when you edit your files you give them a unique name, and it also ensures new uploads get their filename converted to something unique using filebrowsers convert_filename method defined in filebrowser/utils.py

这将确保当您编辑您的文件时,您给它们一个惟一的名称,并且它还确保使用filebrowser/utils.py中定义的filebrowser convert_filename方法将新的上传文件的文件名转换为惟一的文件名

More on filebrowser settings here. Hope this helps :)

更多关于文件浏览器设置。希望这有助于:)

#1


1  

In filebrowser/sites.py you can create a hook for this when uploading / handling uploads:

在filebrowser /网站。py上传/处理上传时可以为此创建一个挂钩:

def _upload_file(self, request):
    """
    Upload file to the server.
    """
    if request.method == "POST":
        folder = request.GET.get('folder', '')

        if len(request.FILES) == 0:
            return HttpResponseBadRequest('Invalid request! No files included.')
        if len(request.FILES) > 1:
            return HttpResponseBadRequest('Invalid request! Multiple files included.')

        filedata = list(request.FILES.values())[0]

        fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
        folder = fb_uploadurl_re.sub('', folder)

        path = os.path.join(self.directory, folder)
        # we convert the filename before uploading in order
        # to check for existing files/folders
        file_name = convert_filename(filedata.name)
        filedata.name = file_name
        file_path = os.path.join(path, file_name
        ....

You can modify file_path here to whatever you like, or modify the file name.

您可以在这里修改file_path到任何您喜欢的地方,或者修改文件名。

For those of you that just want to ensure that files are not overwritten, you can set the FILEBROWSER_OVERWRITE_EXISTING flag in your settings.py as such:

对于那些只想确保文件不被覆盖的人,可以在设置中设置FILEBROWSER_OVERWRITE_EXISTING标记。py是这样的:

FILEBROWSER_OVERWRITE_EXISTING = False

This will ensure that when you edit your files you give them a unique name, and it also ensures new uploads get their filename converted to something unique using filebrowsers convert_filename method defined in filebrowser/utils.py

这将确保当您编辑您的文件时,您给它们一个惟一的名称,并且它还确保使用filebrowser/utils.py中定义的filebrowser convert_filename方法将新的上传文件的文件名转换为惟一的文件名

More on filebrowser settings here. Hope this helps :)

更多关于文件浏览器设置。希望这有助于:)