[英]Google App Engine: Download a file to user's local Download folder
我无法在 Google App Engine 中将文件下载到用户的本地下载文件夹。 我想我已经按照有关如何执行此操作的说明进行操作。 这是服务器代码,其中的标头语句应该使响应转到用户的下载文件夹。 相反,它被发送到发出 GET 请求的 JavaScript 函数,与注释掉 headers 语句的行为相同。 虽然我不知道有什么区别,但 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)
解决方案的链接并没有立即回答我的问题,但最终导致了一个事实,即我需要在 JS 文件中做的就是在 apiGet(route, function(ret) 中插入语句 window.location = route。谢谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.