views:

34

answers:

2

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 trouble 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.

A: 

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')
JiminyCricket
Didn't help. Additionaly I receive the file with self.request.get with no problem.
so are you receiving the file data, or the file name?
JiminyCricket
`.get` is a method of the request object seen as a dict-like mapping; it has no relation with the HTTP method in use, GET or POST!
Alex Martelli
self.request.get('subs_file') <-- gives me file with data (actually only DATA It's weird but I think It's because of GAE) self.request.get('file_name') <-- gives me nothing
A: 

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?

Alex Martelli
I'm so ashamed... Because of untidiness I was modifing wrong method which body and name were quite similiar to the good one...Umm sorry and thanks for your effort.
@user, no shame -- to err is human, and since (believe it or not) most programmers _are_ human, we all err!-) Spotting, admitting and fixing the errors we inevitably make is the mark of excellence, since it can't be "never make any mistake in the first place";-).
Alex Martelli