0
我使用的是Google App Engine(python),並且在服務器上有可用的PNG圖像的數據:url。 PNG圖像從來不在文件中,因爲它是使用toDataUrl()從一些畫布代碼生成的,並且被添加到服務器。我想讓用戶點擊一個按鈕,並能夠選擇一個文件名並保存在本地PNG圖像。另存爲對話框將提供默認的文件名。目標瀏覽器是FireFox。我提供的示例代碼不起作用。在stackoverflow上有幾個問題有點像這個,但每個都有點不同。保存數據:本地使用Google App Engine的PNG圖像的網址
我使用建議的文件名將content-disposition設置爲附件。我將頭文件content-type設置爲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()
{{title}}
我嘗試GET而不是POST,但最終結果總是:localhost:8080 /%ED%BF%BDPNG HTTP /1.0 404未找到。我沒有發送任何數據到服務器,只是得到一個data:url返回。我一定在做一些根本錯誤的事情。 – RFF 2012-04-24 14:53:09