-1

I'm not able in Google App Engine to download a file to the user's local Download folder. I think I've followed the instructions on how to do this. Here is the server code, in which the headers statement is supposed to make the response go to the user's Download folder. Instead, it is sent to the JavaScript function that made the GET request, the same behavior one gets with the headers statement commented out. And though I don't know what the difference is, self.response.out.write and self.response.write behave the same. In addition to get, I've also tried put, post, options, and delete, and all of them behave the same. I've run out of permutations to try.

# -*- 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)

Here is the client code that makes the request:

$(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)
    })
})

})

Here is the HTML file:

<!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>

Here is the yaml file:

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

The directory structure is this:

  app.yaml
  api.py
      /ide (ide.js and index.html)
      /lib (contains the jquery library)
user1114907
  • 972
  • 8
  • 15
  • The link to a solution didn't instantly answer my question but did lead eventually to the fact that all I needed to do in the JS file was to insert in apiGet(route, function(ret) the statement window.location = route. Thank you! – user1114907 Mar 25 '18 at 00:31

1 Answers1

0

The link to a solution didn't instantly answer my question but did lead eventually to the fact that all I needed to do in the JS file was to insert in apiGet(route, function(ret) the statement window.location = route. Thank you!

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
user1114907
  • 972
  • 8
  • 15