我爲當前項目編寫了自定義Django文件上傳處理程序。這是一個概念驗證,它允許您計算上傳文件的散列,而不將該文件存儲在磁盤上。可以肯定的是,這是一個概念證明,但如果我能把它運用起來,我就可以實現我工作的真正目的。訪問自定義Django上傳處理程序中的其他表單域
從本質上講,這是我到目前爲止,這是工作的罰款有一個主要的例外:
from django.core.files.uploadhandler import *
from hashlib import sha256
from myproject.upload.files import MyProjectUploadedFile
class MyProjectUploadHandler(FileUploadHandler):
def __init__(self, *args, **kwargs):
super(MyProjectUploadHandler, self).__init__(*args, **kwargs)
def handle_raw_input(self, input_data, META, content_length, boundary,
encoding = None):
self.activated = True
def new_file(self, *args, **kwargs):
super(MyProjectUploadHandler, self).new_file(*args, **kwargs)
self.digester = sha256()
raise StopFutureHandlers()
def receive_data_chunk(self, raw_data, start):
self.digester.update(raw_data)
def file_complete(self, file_size):
return MyProjectUploadedFile(self.digester.hexdigest())
自定義上傳處理器的偉大工程。散列是準確的,並且不需要將任何上傳的文件存儲到磁盤,並且在任何時候都只使用64kb的內存。
我遇到的唯一問題是,在處理文件(用戶輸入的文本鹽)之前,我需要從POST請求訪問另一個字段。我的形式如下:請求已被處理,該文件已被上傳,它不適合我的使用情況工作崗位
<form id="myForm" method="POST" enctype="multipart/form-data" action="/upload/">
<fieldset>
<input name="salt" type="text" placeholder="Salt">
<input name="uploadfile" type="file">
<input type="submit">
</fieldset>
</form>
「鹽」 POST變量只提供給我。我似乎無法找到一種方法來以任何方式,形狀或形式在我的上傳處理程序中訪問此變量。
有沒有辦法讓我訪問每個多部分變量,而不是隻訪問上傳的文件?
這就是問題所在,你的代碼不起作用。如果我打印self.request.REQUEST,我會在控制檯中獲得'{}',直到處理程序完成後,基本上沒有可用的變量。 – 2013-03-13 00:34:22
如果我用'handle_raw_input'弄髒我的手,我可以讀取實際的請求主體,可變數據和全部。但是,我想知道是否有更好的解決方案,因爲手動處理數據,解析邊界,提取可變信息等有點麻煩。 – 2013-03-13 00:39:10