what I’m trying to do is have a upload button to upload files to our storage system. I’m using Google App Engine with Python. Also HTML and Javascript for views.
For that, we have a HTML, and a.js that asks the user if he’s sure that he wants to overwrite a file. For that overwriting question, I need to ask the Database to know if it exists,and so if the question should be asked or not…
The thing is I don’t know even where to start. I have this confirm() text shown to the user, and a GQL database, but I don’t know how to make a question. For example, I upload via a URL, but then I don’t have a response for that, and also I don’t want to pass a question (name of the file,…) to the database via URL…
Do you have any idea of what path should I follow? Am I trying something impossible or without any sense?
Thanks a lot!
I add some code:
this is the HTML form where we ask the user to upload a file:
<form id="up_file" enctype="multipart/form-data" method="post">
<input type="hidden" name="user_id" value="{{ current_user.id }}"/>
<input type="hidden" name="group_id" value="{{ group.id }}"/>
<p>File: <input type="file" name="filename" id="file_name"/></p>
<p><input type="button" value="Upload" onClick="seguro_sobreescribir(filename,{{ current_user.id }},{{ group.id }})"/></p>
</form>
and this is the javascript that currently tries to send information to our application in google engine when someone clicks the upload button:
function Request(function_name, opt_argv) {
if (!opt_argv)
opt_argv = new Array();
// Find if the last arg is a callback function; save it
var callback = null;
var len = opt_argv.length;
if (len > 0 && typeof opt_argv[len-1] == 'function') {
callback = opt_argv[len-1];
opt_argv.length--;
}
var async = (callback != null);
// Build an Array of parameters, w/ function_name being the first parameter
var params = new Array(function_name);
for (var i = 0; i < opt_argv.length; i++) {
params.push(opt_argv[i]);
}
var body = JSON.stringify(params);
// Create an XMLHttpRequest 'POST' request w/ an optional callback handler
var req = new XMLHttpRequest();
req.open('POST', 'https://safeshareapp.appspot.com/upload', async);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", body.length);
req.setRequestHeader("Connection", "close");
if (async) {
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 200) {
var response = null;
try {
response = JSON.parse(req.responseText);
} catch (e) {
response = req.responseText;
}
callback(response);
}
}
}
// Make the actual request
req.send(body);
}
// Comprobar si existe, y si existe, preguntar si se quiere sobreescribir
function seguro_sobreescribir(filename,user_id,group_id)
{
var resp=confirm("Seguro que quiere sobreescribir el archivo "+filename.value+" del usuario "+user_id+" del grupo "+group_id+"?");
if(resp)
{
var result = Request('Upload',[filename,user_id,group_id]);
alert("Hemos hecho request "+ result);
}
}
And this is the RequestHandler that should handle our request:
class RPCHandler(webapp.RequestHandler):
""" Allows the functions defined in the RPCMethods class to be RPCed."""
def __init__(self):
webapp.RequestHandler.__init__(self)
self.methods = RPCMethods()
def post(self):
args = simplejson.loads(self.request.body)
func, args = args[0], args[1:]
if func[0] == '_':
self.error(403) # access denied
return
func = getattr(self.methods, func, None)
if not func:
self.error(404) # file not found
return
result = func(*args)
self.response.out.write(simplejson.dumps(result))
class RPCMethods:
def Upload(self, *args):
status = -1
fileitem = args[0]
userid = args[1]
groupid=args[2]
return status
def main():
app = webapp.WSGIApplication([('/upload', RPCHandler)], debug=True)
util.run_wsgi_app(app)
if __name__ == '__main__':
main()
The fact is that the return status gives us a undefined back at the javascript.
We don’t know if we are not uploading the file and if so, how to do it. That’s because we have 2 things that we don’t know how to put together:
-
The normal “input type=file, method=post, and submit input type=submit button of a HTML form
-
Our connection via RequestHandler to the google app engine etc.
Do you have any ideas?
Here’s a possibility:
Implement a servlet that can answer the question given a filename; it could return a ‘0’ or ‘1’ (or whatever you choose) as its HTTP response depending on whether the file exists. Then make an XmlHttpRequest POST to that servlet from your javascript with the filename as a POST parameter. Depending on the return of the XmlHttpRequest, show UI to the user asking to confirm.