【问题标题】:Saving a data:url of a PNG image locally using Google App Engine使用 Google App Engine 在本地保存 PNG 图像的 data:url
【发布时间】:2012-05-04 14:55:01
【问题描述】:

我正在使用 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()

{{标题}}

【问题讨论】:

    标签: google-app-engine content-type savefiledialog content-disposition data-url


    【解决方案1】:

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

    【讨论】:

    • 我尝试了 GET 而不是 POST,但最终结果始终是:localhost:8080/%ED%BF%BDPNG HTTP/1.0 404 Not Found。我没有向服务器发送任何数据,只是得到一个 data: url 返回。我一定是做错了什么。
    猜你喜欢
    • 2023-03-22
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2010-11-09
    • 2011-11-19
    • 1970-01-01
    相关资源
    最近更新 更多