Django文件上传进度获取缓存返回无

标签 django caching file-upload progress

Django文件上传进度处理json请求得到空响应

view.py

def upload_progress(request):
    """
    A view to report back on upload progress.
    Return JSON object with information about the progress of an upload.

    Copied from:
    http://djangosnippets.org/snippets/678/

    See upload.py for file upload handler.
    """
    #import ipdb
    #ipdb.set_trace()
    progress_id = ''
    if 'X-Progress-ID' in request.GET:
        progress_id = request.GET['X-Progress-ID']
    elif 'X-Progress-ID' in request.META:
        progress_id = request.META['X-Progress-ID']
    if progress_id:
        from django.utils import simplejson
        cache_key = "%s_%s" % (request.META['REMOTE_ADDR'], progress_id)
        data = cache.get(cache_key)


        return HttpResponse(simplejson.dumps(data))

UploadProgressCachedHandler.py

    from django.core.files.uploadhandler import FileUploadHandler
    from django.core.cache import cache

    class UploadProgressCachedHandler(FileUploadHandler):
        """
        Tracks progress for file uploads.
        The http post request must contain a header or query parameter, 'X-Progress-ID'
        which should contain a unique string to identify the upload to be tracked.

        Copied from:
        http://djangosnippets.org/snippets/678/

        See views.py for upload_progress function...
        """

        def __init__(self, request=None):
            super(UploadProgressCachedHandler, self).__init__(request)
            self.progress_id = None
            self.cache_key = None

        def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None):
            self.content_length = content_length
            if 'X-Progress-ID' in self.request.GET :
                self.progress_id = self.request.GET['X-Progress-ID']
            elif 'X-Progress-ID' in self.request.META:
                self.progress_id = self.request.META['X-Progress-ID']
            if self.progress_id:
                self.cache_key = "%s_%s" % (self.request.META['REMOTE_ADDR'], self.progress_id )
                cache.set(self.cache_key, {
                    'length': self.content_length,
                    'uploaded' : 0
                })

        def new_file(self, field_name, file_name, content_type, content_length, charset=None):
            pass

        def receive_data_chunk(self, raw_data, start):
            if self.cache_key:
                data = cache.get(self.cache_key)
                data['uploaded'] += self.chunk_size
                cache.set(self.cache_key, data)
                #cache.set(self.cache_key, 5000)
            return raw_data

        def file_complete(self, file_size):
            pass

        def upload_complete(self):
            if self.cache_key:
                cache.delete(self.cache_key)

我正在使用 uploadProgressCacheHandler 设置缓存。但是当尝试通过 json 请求检索时。data 返回 None 对象。'cache_key' 生成正确。

请帮忙。

最佳答案

我猜您是在流已达到 upload_complete 时尝试发出 JSON 请求。此时 cache_key 已从缓存中删除(这就是它为空的原因)。

您使用的是 Django 开发服务器吗?我认为这个内置服务器一次只允许处理一个请求,在您的情况下这意味着首先完成上传并处理 JSON 请求。您可以尝试使用另一台能够处理多个请求的服务器(例如:Apache)。

关于Django文件上传进度获取缓存返回无,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12156207/

相关文章:

python - django settings.py os.environ.get ("X") 没有获取正确的值

Django 不反射(reflect)对应用程序所做的更改

Wordpress 和 W3 Total Cache minifier

android - 如何让android删除SD卡上的缓存?

jquery - 如何将项目 Expressjs 与 jQuery-File-Upload 集成?

Laravel 9 Livewire 照片上传失败

django - Django 1.4 datefield和sqlite问题

python - 为什么找不到Dockerfile?

ruby-on-rails - 什么会导致 Rails 页面缓存停止工作?

c# - 文件上传在 ASP.NET MVC4 中不起作用 - 移动模板