如何替换/覆盖上传的文件? [英] How can I replace/override an uploaded file?

查看:449
本文介绍了如何替换/覆盖上传的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够上传文件并在每次上传时覆盖/替换最新版本的现有文件。

I want to be able to upload a file and on each upload to override/replace the existing file with the newest version.

from django.core.files.storage import FileSystemStorage  
fs = FileSystemStorage(location='C:/temp', base_url='/attachments')      
class Import(models.Model):  
    file = models.FileField(upload_to='data', storage=fs)


推荐答案

我不知道这是否是最好的方法,但以下行帮助我覆盖/替换现有文件。

I don't know if this is the best approach, but the following lines helped me override/replace the existing file.

upload_dir_path = Setting.objects.get(entry__exact='upload_path').value
delete_files(upload_dir_path)
upload = form.save(commit=False)
upload.file.storage.location = upload_dir_path            
upload = form.save()

这篇关于如何替换/覆盖上传的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆