簡體   English   中英

金字塔+ ZODB圖像存儲

[英]Pyramid + ZODB Image storing

我有一個上傳表單,接受一個zip文件,並有一個方法解壓縮它並從中獲取每個文件。 從md5哈希中創建一個唯一的id並將它們存儲在字典中;

dict[uid] = imagebinary

並返回它,以便表單可以將它們存儲到ZODB中。 我不能像那樣存儲圖像,因為這個錯誤吐出來了;

    2013-01-31 08:59:59,061 ERROR [waitress][Dummy-5] Exception when serving /
Traceback (most recent call last):
  File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/waitress-0.8.2-py2.7.egg/waitress/channel.py", line 329, in service
    task.service()
  File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/waitress-0.8.2-py2.7.egg/waitress/task.py", line 173, in service
    self.execute()
  File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/waitress-0.8.2-py2.7.egg/waitress/task.py", line 380, in execute
    app_iter = self.channel.server.application(env, start_response)
  File "/home/maverick/.buildout/eggs/pyramid-1.4-py2.7.egg/pyramid/router.py", line 251, in __call__
    response = self.invoke_subrequest(request, use_tweens=True)
  File "/home/maverick/.buildout/eggs/pyramid-1.4-py2.7.egg/pyramid/router.py", line 227, in invoke_subrequest
    response = handle_request(request)
  File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 133, in toolbar_tween
    body = tb.render_full(request).encode('utf-8', 'replace')
  File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/tbtools.py", line 240, in render_full
    summary = self.render_summary(include_title=False, request=request)
  File "/home/maverick/inigo/sources/devenv/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/tbtools.py", line 229, in render_summary
    'description':  description_wrapper % escape(self.exception),
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 114: ordinal not in range(128)

那么,我該怎么做呢? 我幾乎堅持這個。

您看到的錯誤與ZODB中存儲的圖像無關。

要存儲更大的數據,您確實希望使用ZODB Blob而不是將圖像數據直接放在屬性中。 Blob s單獨存儲在磁盤上,不會刷新ZODB緩存,並且可以再次訪問時再流回客戶端。

要創建和存儲Blob ,請使用:

from ZODB.blob import Blob

uid = Blob(imagebinary.read())

一旦這樣創建,您可以稍后使用uid作為文件; 你需要先在讀或寫模式下打開它。 例如,要從視圖返回blob的內容,請使用:

from pyramid.response import Response

def serveimage(request):
    # retrieve uid from somewhere
    resp = Response(content_type='image/jpeg')
    resp.app_iter = uid.open('r')  # open for reading
    return resp

Blob綁定到事務,如果事務回滾,則會自動丟棄對它們的更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM