我正在將多個 pdf 上傳從表單傳遞到視圖中。(使用 Uppy.XHRUpload)
我想在將它們保存在模型中之前對其進行加密。
當我測驗檔案時,它們可以被加密并保存到檔案中,然后讀取和解密就好了。
但是當我嘗試添加到模型時,我得到:
'bytes' object has no attribute '_committed' occurred.
我可以下載加密檔案,重新閱讀然后保存,但這將是一種浪費。
我認為這很簡單:
if request.method == 'POST' and request.FILES:
files = request.FILES.getlist('files[]')
for index, file in enumerate(files):
f = Fernet(settings.F_KEY)
pdf = file.read()
encrypted = f.encrypt(pdf)
PDF_File.objects.create(
acct = a,
pdf = encrypted
)
該模型。
class PDF_File(models.Model):
acct = models.ForeignKey(Acct, on_delete=models.CASCADE)
pdf = models.FileField(upload_to='_temp/pdf/')
謝謝你的幫助。
uj5u.com熱心網友回復:
這是因為您無法將加密(位元組)保存到模型中
嘗試這個
from django.core.files.base import ContentFile
for index, file in enumerate(files):
f = Fernet(settings.F_KEY)
pdf = file.read()
encrypted = f.encrypt(pdf)
content_file = ContentFile(encrypted, name=your_filename)
PDF_File.objects.create(
acct = a,
pdf = content_file
)
從這里參考https://docs.djangoproject.com/en/4.0/topics/http/file-uploads/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/434706.html
