我正在使用 Google App Engine (python) 并且有一个数据:服务器上可用的 PNG 图像的 url。PNG 图像从未在文件中,因为它是使用 toDataUrl() 从一些画布代码生成的,并通过 ajax 处理到服务器。我想允许用户单击一个按钮并能够选择一个文件名并在本地保存 PNG 图像。Save As 对话框将提供一个默认的 filename.png。目标浏览器是 FireFox。我提供了不起作用的示例代码。stackoverflow 上有几个问题有点像这个,但每个问题都有点不同。
我将内容处置设置为带有建议文件名的附件。我将标头内容类型设置为 application/octet-stream。但我没有得到 SaveAs 对话框。我错过了什么?
app.yaml 文件是标准的
application: saveas
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
index.html 如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script>
function saveAsPng()
{
var alldata;
var httpRequest;
var response;
var myheaders;
httprequest = new XMLHttpRequest();
if ( !httprequest )
{
alert("In saveAsPng, XMLHttpRequest failed.");
return;
}
/* Make this a json string */
alldata = JSON.stringify("no data");
try
{
httprequest.open('POST', '/handlebitmap', true);
httprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httprequest.setRequestHeader("Content-length", alldata.length);
httprequest.setRequestHeader("Connection", "close");
httprequest.onreadystatechange = function()
{
if ( httprequest.readyState == 4 )
{
if (httprequest.status == 200)
{
/* a status of 200 is good. */
response = null;
try
{
response = JSON.parse(httprequest.responseText);
}
catch (e)
{
response = httprequest.responseText;
}
if ( response == "error" )
{
alert("In saveAsPng callback, response == error");
return false;
}
else
{
/* This is the successful exit. */
//alert("response = " +response);
window.location.href = response;
return true;
}
}
else
{
/* httprequest.status was not 200, so must be an error. */
alert("saveAsPNG callback, status = " +httprequest.status);
return false;
}
} /* End of if where readyState was 4. */
} /* End of the callback function */
/* Make the actual request */
httprequest.send(alldata);
}
catch(e)
{
alert("In saveAsPng, Can't connect to the server");
}
} /* End of the saveAsPng function */
python代码如下:
# !/usr/bin/env python
import os
import base64
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
class MainPage(webapp.RequestHandler):
""" Renders the main template."""
def get(self):
template_values = { 'title':'Test Save As PNG', }
path = os.path.join(os.path.dirname(__file__), "index.html")
self.response.out.write(template.render(path, template_values))
class BitmapHandler(webapp.RequestHandler):
""" Shows the Save As with a default filename. """
def post(self):
origdata = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
urldata = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
decodeddata = base64.b64decode(urldata)
self.response.headers['Content-Type'] = 'application/octet-stream'
self.response.headers['Content-Disposition'] = 'attachment; filename="untitled.png"'
self.response.out.write(decodeddata)
def main():
app = webapp.WSGIApplication([
('/', MainPage),
('/handlebitmap',BitmapHandler),
], debug=True)
util.run_wsgi_app(app)
if __name__ == '__main__':
main()
{{标题}}