-1

I am writing a web app and I need to read a text file that a user will upload.

I will not need to store it in a database; the app only performs simple operations on the text in the file.

I'm currently uploading the file with <input type=file" name="input_file" id="file-in"> in a form submitted via POST and running this in main.py:

user_file = self.request.get("input_file")
print user_file
print type(user_file)

Output when I tried to upload a file ("foo.txt") with the contents "The quick brown fox jumps over the lazy dog!":

foo.txt
<type 'unicode'>

Why did this happen? How can I access the contents of the file?

Any help is appreciated. If you need some background info, I wrote this simple encryptor/decryptor app, and I'm trying to add the ability to upload a file instead of just copy-pasting the text in.

nick818
  • 41
  • 4

2 Answers2

1

Give your input a name:

<input type="file" name="input_file"/> 

and then use the name rather than input in your call to request.get():

user_file = self.request.get("input_file")

See also: Upload files in Google App Engine

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Oops! That was actually an error in my question asking– I will edit the question now. I had ``; I changed it to `` and adjusted my call to `request.get()`. Nothing changed. – nick818 Dec 01 '13 at 06:50
  • Suggestion: Carefully check the rest of your code against the good answers in the "See also" link I provided ([Upload files in Google App Engine](http://stackoverflow.com/questions/81451/upload-files-in-google-app-engine)). – kjhughes Dec 01 '13 at 07:08
0

Try:

user_file = self.request.get("input")
print user_file
print type(user_file)

print user_file.decode('utf-8')
print type(user_file.decode('utf-8'))
Yohann
  • 6,047
  • 3
  • 26
  • 33