Google App Engine:将文件下载到用户的本地下载文件夹

时间:2018-03-24 01:03:46

标签: google-app-engine

我无法在Google App Engine中将文件下载到用户的本地下载文件夹。我想我已经遵循了如何做到这一点的说明。这是服务器代码,其中headers语句应该使响应转到用户的Download文件夹。相反,它被发送到发出GET请求的JavaScript函数,与header语句注释掉的相同行为。虽然我不知道区别是什么,但self.response.out.write和self.response.write的行为相同。除了get之外,我还尝试了put,post,options和delete,并且所有这些行为都相同。我已经用尽了排列试试。

# -*- coding: utf-8 -*-
import webapp2 as web
class ApiUser(web.RequestHandler):
    def get(self, user): # fails with get, put, post, options, delete
        source = 'a = 5'
        self.response.headers['Content-Disposition'] = 'attachment; filename='+'test.py' # no effect
        self.response.write(source)  # same effect as self.response.out.write(source)
app = web.WSGIApplication([ (r'/api/user/([^/]+)', ApiUser) ], debug=True)

以下是发出请求的客户端代码:

$(function () {
"use strict";
function apiError(message) { console.log('ERROR', message) }

function apiGet(route, callback) {
    $.ajax({
        type: 'GET',
        url: route,
        //dataType: 'json', // SyntaxError: Unexpected token a in JSON at position 0
        success: callback,
        error: function (xhr, message, exc) {
            apiError("API " + message + " getting " + url + ": " + exc)
        }
    })
}

$('#submit').click(function() {
    var route = "api/user/"+'Testing'
    apiGet(route, function(ret) {
        console.log('callback', ret)
    })
})

})

这是HTML文件:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a>JQuery Test Page</a><br>
<input id="submit" type="button" value="Submit"/>
<script type="text/javascript" language="javascript" src="lib/jquery/IDE/jquery.min.js"></script>
<script type="text/javascript" language="javascript" src="ide.js"></script>
</body>
</html>

这是yaml文件:

application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:

- url: /
  static_files: ide/index.html
  upload: ide/index.html

- url: /ide.js
  static_files: ide/ide.js
  upload: ide/ide.js

- url: /lib
  static_dir: lib

- url: /api/.*
  script: api.app

- url: /favicon\.ico
  static_files: static/images/favicon.ico
  upload: static/images/favicon\.ico

目录结构如下:

  app.yaml
  api.py
      /ide (ide.js and index.html)
      /lib (contains the jquery library)

1 个答案:

答案 0 :(得分:0)

解决方案的链接没有立即回答我的问题,但最终确实导致我在JS文件中需要做的就是插入apiGet(路由,函数(ret)语句窗口)。 location = route。谢谢!