开发者

How to handle direct file upload with GAE/python?

开发者 https://www.devze.com 2023-03-23 09:52 出处:网络
Client side (javascript) uploads the application with XMLHttpRequ开发者_运维技巧est: var req = new XMLHttpRequest();

Client side (javascript) uploads the application with XMLHttpRequ开发者_运维技巧est:

  var req = new XMLHttpRequest();
  req.open('POST', my_app_url, false);
  req.setRequestHeader("Content-Length", req.length);
  req.sendAsBinary(req.binary);

I use datastore on the server side (not blobstore). How can I save uploaded file to the datastore? I've found that ServletFileUpload can be used with Java. But how to do the same with Python?


You should use self.request.body

class YourUploadHandler(webapp.RequestHandler):
    def post(self):
        your_binary_content = self.request.body


If you mean on the appengine side, you just have to have a blobproperty. So something like...

class SomeEntity(db.Model):
    file_data = db.BlobProperty()

class AddData(webapp.RequestHandler)
    def post(self):
        data = self.request.get("filedata")
        e = SomeEntity(file_data = db.Blob(data))
        e.put()

As a note, I'm not sure if the code you posted above to send the request is correct, but you can upload the file with a simple html form, something like this:

<form action="/url_to_adddata_handler/" method="post">
    <input type="file" name="filedata">
    <input type="submit" value="Submit">
</form>
0

精彩评论

暂无评论...
验证码 换一张
取 消