appengine发送数据作为文件下载 [英] appengine send data as a file download

查看:109
本文介绍了appengine发送数据作为文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python和appengine的新手。所以我有这个简单的问题。我想发送服务器数据并让服务器将它作为文件下载发送回去。如果我以前做过,我可以下载一个文件一个页面加载。但在此之后,我无法下载。这是我现在所拥有的:

I'm new to python and appengine.So I have this simple problem.I want to send the server data and have the server send it back as a file download.I can get a file to download if I do it before a page loads. But after that I can't get the download.Here is what I have right now:

class UploadHandler(webapp.RequestHandler):

  def get(self):
    try:    
        xml=parseString(self.request.body)
        result=sanitize(xml)
        if result == None:
          text=xml.toxml('UTF-8')
          name="file.xml"   
          self.response.out.write(text)
          self.response.headers.add_header('Method','get')
          self.response.headers.add_header('Content-Type','text/xml')
          self.response.headers.add_header('name',name)
          self.response.headers.add_header('Content-Disposition','attachment')
          self.response.headers.add_header('filename',name)
          self.response.headers.add_header('Content',text)
   except:
        print self.request.body

以下是调用它的javascript:

And here is the javascript to call it:

        var text=getMarkup();
        new Ajax.Request("http://localhost:8080/file",{
        method:'get',
        contentType:"text",
        postBody:text,
        onSuccess:function(){
            console.log(transport.responseText);            
        },          
        onFailure: function(){ 
            console.log('Could not load file...'); 
        },
        onException: function(req,exception) {
            console.log(exception);
            return true;
            }, 
        });

我试着用两种方法改变了许多不同的方式。我可以得到回应,但我可以'如果我在主页面上使用相同的代码,它可以工作。我错过了什么?

I've tried changing both pieces many different ways.I can get a response, but I can't get it to go to the downloads.If I use the same code for the main page it works though.What am I missing?

终于明白了....
我使用此页面上的示例此处
只有我发回url作为响应,并在onsuccess处理程序中使用window.open打开它。

Finally got it.... I use the example on this page here only I sent the url back as the response and used window.open in the "onsuccess" handler to open it.

blobstore的问题在于文件保存在服务器上,其他人可以查看它们,所以我最好的解决方案就是使用全局变量来保存数据,然后通过它来获取并调用从window.open()获取javascript。

The problem with the blobstore is that the files are saved on the server and other people can view them, so my best solution is just to use a global variable to save the data from the post and then pass it to get and call get from javascript with window.open().

推荐答案

我觉得这个将帮助

I think this will help

import mimetypes
...... 

(content_type, _) = mimetypes.guess_type(name)
self.response.headers['Content-Type'] = content_type
self.response.headers['Content-Disposition'] = 'attachment; filename=' + name
self.response.out.write(text) 

办法。您也可以使用blobstore保存您的文件并下载blob文件。

By the way. You can also use the blobstore to save your file and download the blob file.

这篇关于appengine发送数据作为文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆