开发者

Trouble with receiving data from <form>

HTML:

<form enctype="multipart/form-data" action="/convert_upl" method="post">          
         Name:  <input type="text" name="file_name">
         File:  <input type="file" name="subs_file"> 
        <input type="submit" value="Send">
</form>

Python (Google App Engine):

if self.request.get('file_name'):
                    file_name = self.request.get('file_name')

My problem is that I receive no data from file_name text input. I am aware that the troubl开发者_C百科e is because of it's existence within the form enctype="multipart/form-data" but I don't know how to solve it - I mean how to receive a file and the string from the input with one click of the submit button.

Thanks in advance.


The uploading example code works fine for me. Have you tried using that code exactly? Does it work for you, or what problems do you see?

As you'll see, that example has a form with the same encoding you're using:

      <form action="/sign" enctype="multipart/form-data" method="post">
        <div><label>Message:</label></div>
        <div><textarea name="content" rows="3" cols="60"></textarea></div>
        <div><label>Avatar:</label></div>
        <div><input type="file" name="img"/></div>
        <div><input type="submit" value="Sign Guestbook"></div>
      </form>

it's just a bit more careful in the HTML to properly use label tags to display field labels, but that only affect the form's looks when rendered in the browser.

The Python code is also similar to what you show (for the tiny susbset that you do show):

def post(self):
    greeting = Greeting()
    if users.get_current_user():
        greeting.author = users.get_current_user()
    greeting.content = self.request.get("content")
    avatar = self.request.get("img")
    greeting.avatar = db.Blob(avatar)
    greeting.put()
    self.redirect('/')

and of course the /sign URL is directed to the class whose do_post method we've just shown.

So, if this code works and yours doesn't, where is the difference? Not in the part you've shown us, so it must be in some parts you haven't shown... can you reproduce the part about this example code from Google working just fine?


You are using the POST method to send the data but then are trying to get it with the GET method.

instead of

self.request.get('file_name')

do something like

self.request.post('file_name')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜