【问题标题】:how to stream file to client in django如何在 django 中将文件流式传输到客户端
【发布时间】:2012-12-01 05:49:34
【问题描述】:

我想知道如何使用 django 将数据流式传输到客户端。

目标

用户提交表单,表单数据被传递给返回字符串的 Web 服务。字符串被打包 (tar.gz) 并且 tarball 被发送回给用户。

我不知道是什么方法。我搜索并找到this,但我只有一个字符串,我不知道它是否是我想要的东西,我不知道用什么代替filename = __file__,因为我没有文件 - 只是一个字符串。如果我为每个用户创建一个新文件,这将不是一个好方法。所以请帮助我。 (对不起,我是网络编程新手)。

编辑:

$('#sendButton').click(function(e) {
        e.preventDefault();
        var temp = $("#mainForm").serialize();
        $.ajax({
            type: "POST",
            data: temp,
            url: 'main/',
            success: function(data) {                
                $("#mainDiv").html(data.form);
                ????                

            }
        });
    });

我想使用ajax,那么我应该怎么做才能成功使用ajac函数并返回视图。真的谢谢。

我的观点.py:

def idsBackup(request):
    if request.is_ajax():        
        if request.method == 'POST':
           result = ""
           form = mainForm(request.POST)
           if form.is_valid():
               form = mainForm(request.POST)
               //do form processing and call web service               

                    string_to_return = webserviceString._result 
                    ???
           to_json = {}
           to_json['form'] = render_to_string('main.html', {'form': form}, context_instance=RequestContext(request))
           to_json['result'] = result
           ???return HttpResponse(json.dumps(to_json), mimetype='application/json')
        else:
            form = mainForm()
        return render_to_response('main.html', RequestContext(request, {'form':form}))
    else:
        return render_to_response("ajax.html", {}, context_instance=RequestContext(request))

【问题讨论】:

  • 您从 Web 服务收到的字符串是否已经是 tar.gz 文件?还是您想获取字符串,将其压缩并发送结果?
  • 不,不是。我收到字符串并将其编码为base64,然后我想将其打包并发送结果。
  • 啊哈,它似乎不适用于 ajax,我需要这样做。我不知道如何欣赏你。如果我找不到解决方案,也许我会创建一个新帖子,如果您知道该怎么做,我会很高兴为您提供帮助 :)

标签: django file django-views filestream


【解决方案1】:

您可以使用字符串内容而不是实际文件来创建 ContentFile 的 django 文件实例,然后将其作为响应发送。

示例代码:

from django.core.files.base import ContentFile
def your_view(request):
    #your view code
    string_to_return = get_the_string() # get the string you want to return.
    file_to_send = ContentFile(string_to_return)
    response     = HttpResponse(file_to_send,'application/x-gzip')
    response['Content-Length']      = file_to_send.size    
    response['Content-Disposition'] = 'attachment; filename="somefile.tar.gz"'
    return response   

【讨论】:

    【解决方案2】:

    您可以从 sn-p 修改 send_zipfile 以满足您的需要。只需使用 StringIO 将您的字符串转换为可以传递给 FileWrapper 的类似文件的对象。

    import StringIO, tempfile, zipfile
    ...
    # get your string from the webservice
    string = webservice.get_response() 
    ...
    temp = tempfile.TemporaryFile()
    
    # this creates a zip, not a tarball
    archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    
    # this converts your string into a filelike object
    fstring = StringIO.StringIO(string)   
    
    # writes the "file" to the zip archive
    archive.write(fstring)
    archive.close()
    
    wrapper = FileWrapper(temp)
    response = HttpResponse(wrapper, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=test.zip'
    response['Content-Length'] = temp.tell()
    temp.seek(0)
    return response
    

    【讨论】:

      猜你喜欢
      • 2015-11-29
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 2012-02-10
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      相关资源
      最近更新 更多