使用 Google App Engine 在本地保存 PNG 图片的 data:url



我正在使用Google App Engine(python),并且服务器上有一个PNG图像的数据:网址。PNG 图像从未出现在文件中,因为它是使用 toDataUrl() 从某些画布代码生成的,并被 ajaxed 到服务器。我想允许用户单击一个按钮并能够选择一个文件名并将 PNG 图像保存在本地。"另存为"对话框将提供默认文件名.png。目标浏览器是火狐浏览器。我提供了不起作用的示例代码。关于stackoverflow有几个问题有点像这个问题,但每个问题都有点不同。

我正在使用建议的文件名将内容处置设置为附件。我将标头内容类型设置为应用程序/八位字节流。但是我没有得到"保存为"对话框。我错过了什么?

app.yaml 文件是标准的

application: saveas
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
  script: main.py

索引.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()

{

{标题}}

也许您想将"def post"更改为"def get"?我刚刚尝试了您的示例,并且在更改后它似乎按预期工作(仅尝试了/handlebitmap)

相关内容

  • 没有找到相关文章

最新更新