如何在django中将文件流式传输到客户端

如何在django中将文件流式传输到客户端,django,file,django-views,filestream,Django,File,Django Views,Filestream,我想知道如何使用django将数据流传输到客户端 目标 用户提交一个表单,表单数据被传递到一个返回字符串的web服务。字符串被涂上焦油(tar.gz)并将焦油球发送回用户 我不知道怎么走。我搜索并找到了,但我只有一个字符串,我不知道它是否是我想要的东西,我不知道用什么来代替filename=\uuuuu file\uuuu,因为我没有文件-只有一个字符串。如果我为每个用户创建一个新文件,这将不是一个好方法。所以请帮帮我。(对不起,我是网络编程新手) 编辑: 我想使用ajax,那么在ajac函数成

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

目标

用户提交一个表单,表单数据被传递到一个返回字符串的web服务。字符串被涂上焦油(
tar.gz
)并将焦油球发送回用户

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

编辑:

我想使用ajax,那么在ajac函数成功和视图返回时应该做些什么呢。真的谢谢

my view.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))

您可以使用字符串内容而不是实际文件创建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   

您可以修改代码段中的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

从web服务返回的字符串是否已经是tar.gz文件?或者你想把字符串涂上焦油并发送结果吗?不,不是。我收到字符串并将其编码到base64,然后我想对其进行tarball并发送结果。显然,啊哈,它似乎不适用于ajax,我需要这样做。我不知道如何欣赏你。如果我找不到解决方案,也许我会创建一个新帖子,如果你知道该怎么做,我会很高兴你的帮助:)
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