I have this view:
def index(request):
file = open("SK ✌😜✌.txt", encoding="UTF-8")
data = file.read()
file.close()
lines = data.split("\n")
...More code...
In this view i open a file from the very first moment the app starts and i do some work on the file, is a story, and when i start the server and go to http://127.0.0.1:8000/(Name Of The App), i see all the work that i have done on that file.
What i want to do is to do that same work, starting with the reading of the file, BUT i want to do that with the file that the user uploads in that moment. I have this that i took from bootstrap:
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
</div>
I guess i have to use in some way the id of the input but i`m not really sure how to pass this file that the user uploads in the ui to the method that i have in my views.py
Any help will be really appreciated
You need to have a name attribute in your <input> template code.
<input type="file" id="exampleInputFile" name="some_file">
Then to access the file in your view, you need to use request.FILES attribute.
As per the Django docs on HttpRequest.FILES attribute:
A dictionary-like object containing all uploaded files. Each key in
FILES is the name from the <input type="file" name="" />. Each
value in FILES is an UploadedFile.
Your code should be something like:
def index(request):
if request.method=="POST":
uploaded_file = request.FILES['some_file'] # get the uploaded file
# do something with the file
Note: request.FILES will only contain data if the request method was POST and the <form> that posted to the request had enctype="multipart/form-data. Otherwise, FILES will be a blank dictionary-like object.
This is a function which (in a GET request) receives a case_url and case_key and serves the corresponding case (using mongoDB) to a html template called detail_case.
Im trying to add a feature where when a form is filled(on this same page detail_case) and it is submitted, it should submit a POST request to the same function and the code under 'if request.method=="POST"' should get executed.
#app.route('/case/<case_url>/<case_key>', methods=["GET","POST"])
def serve_case(case_url,case_key):
"""for saving a comment in db.comments"""
if request.method == "POST":
text=request.form['comment_text']
#code which inserts it in the database
return redirect(url_for('serve_case', \
case_url=case_url,\
case_key="Highlights"))
"""
Function serves the case as per the key indicated in the URL
"""
#corresponding code here which fills values of variables and sends it to another page
return render_template('detail_case.html')
The problem is that I don't think the POST request is ever executed. This is the html code on the template page detail_case-
<textarea placeholder="Please enter your comments here" action="{{ url_for('serve_case',case_url=case_url,case_key=case_key)}}" method="POST" name="comment_text" rows="6"></textarea><br />
The problem i think is the action field. I don't know how should I send the variable comment_text to my function. Infact, the code under POST does not get executed when I submit.
Basically the issue is that during a GET request, it sends 2 variables which are needed in the parameters of the function serve_case. During my POST request, well, I don't know how to exactly frame the action field. If I send no parameters, its an error. If I don't send it to the same function, then how will it execute the POST code? Could someone please suggest sumthing?
i'm pretty new to flask, i'm editing someone else's code
You need to submit the POST request (for example through form) like below:
<form action="{{ url_for('serve_case',case_url=case_url,case_key=case_key)}}" method="POST">
<input type="text" placeholder="Please enter your comments here">
<input type="submit" name="comment_text" rows="6"><br />
</form>
Here I am trying to upload a file asynchronously to the blobstore. Following is what I've done so far:
html file
<form id="my_form" enctype="multipart/form-data" method="POST"
action="/partner">
<input type="file" id="my_file" name="my_file"/>
</form>
js file
my.Project.prototype.onFileUpload = function(e) {
var uploadForm = /** #type {HTMLFormElement} */ (
goog.dom.getElement('my_form'));
var iframeIo = new goog.net.IframeIo();
goog.events.listen(iframeIo, goog.net.EventType.COMPLETE, function() { alert('request complete'); });
iframeIo.sendFromForm(uploadForm);
python code
class MyHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
logging.info(self.request) // I can see my file in this line's output
upload_files = self.get_uploads('my_file')
logging.info(upload_files) //upload_files come out to be an empty array
blob_info = upload_files[0]
self.redirect('/partner/serve/%s' % blob_info.key())
Any pointers on how to get the file to be uploaded fron the Request object.
The python code provided by google tutorial on blobstore can be found here.
Now I am stuck. I believe if I can get the file in python code I'll be able to upload it.
Any pointers will be very helpful.
Thanks,
Mohit
This isn't really a question about iframeio, but simply about uploading in AppEngine. What you're missing is that you're supposed to create a URL to upload to first, in your GET method, and use that as the action parameter for the form. See the sample application in the AppEngine docs.
So in your case, you'd do upload_url = blobstore.create_upload_url('/partner'), and in your template, <form action="{{ upload_url }}" method="POST" enctype="multipart/form-data">, etc.
I need to know it to correctly get filedata from POST dictionary.
I want to use Agile Uploader (resizes images on client side before upload on the server) in my project. It has process.php file as example of handling image on server. In that file:
$tmp_name = $_FILES["Filedata"]["file_name"]; // where "tmp_name" is file name
As Pyramid doesn't have FILES dictionary, I supose I have to find image in POST. But when I try to upload image POST is empty...
So where it send that image and how to find it on server side?
HTML (most part of html code taken from their demo):
<form action="/test" method="post" id="singularDemo" enctype="multipart/form-data">
<div id="single"></div>
</form>
Submit
<script type="text/javascript">
$('#single').agileUploaderSingle({
submitRedirect:'',
formId:'singularDemo',
progressBarColor:'#3b5998',
flashVars:{
firebug:true,
form_action:'/test'
}
});
</script>
Python (Pyramid code), just for testing - simple view:
def test(request):
if request.method == 'POST':
pass
return {}
Thanks!
Since the $_FILES global contains files submitted by a POST request, you can access them using request.POST:
# access the filename
filename = request.POST['Filedata'].filename
# access the actual file
input_file = request.POST['Filedata'].file
This is an exact equivalent of the PHP $_FILES variable, so if it doesn't work, something else must be wrong.
The Pyramid cookbook has more information about file uploads.
I am planning to create a web app that allows users to downgrade their visual studio project files. However, It seems Google App Engine accepts files uploading and flat file storing on the Google Server through db.TextProperty and db.BlobProperty.
I'll be glad anyone can provide code sample (both the client and the server side) on how this can be done.
In fact, this question is answered in the App Egnine documentation. See an example on Uploading User Images.
HTML code, inside <form></form>:
<input type="file" name="img"/>
Python code:
class Guestbook(webapp.RequestHandler):
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('/')
Here is a complete, working file. I pulled the original from the Google site and modified it to make it slightly more real world.
A few things to notice:
This code uses the BlobStore API
The purpose of this line in the
ServeHandler class is to "fix" the
key so that it gets rid of any name
mangling that may have occurred in
the browser (I didn't observe any in
Chrome)
blob_key = str(urllib.unquote(blob_key))
The "save_as" clause at the end of this is important. It will make sure that the file name does not get mangled when it is sent to your browser. Get rid of it to observe what happens.
self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)
Good Luck!
import os
import urllib
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
class MainHandler(webapp.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload')
self.response.out.write('<html><body>')
self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""")
for b in blobstore.BlobInfo.all():
self.response.out.write('<li>' + str(b.filename) + '')
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
self.redirect('/')
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, blob_key):
blob_key = str(urllib.unquote(blob_key))
if not blobstore.get(blob_key):
self.error(404)
else:
self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)
def main():
application = webapp.WSGIApplication(
[('/', MainHandler),
('/upload', UploadHandler),
('/serve/([^/]+)?', ServeHandler),
], debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
main()
There is a thread in Google Groups about it:
Uploading Files
With a lot of useful code, that discussion helped me very much in uploading files.
Google has released a service for storing large files. Have a look at blobstore API documentation. If your files are > 1MB, you should use it.
I try it today, It works as following:
my sdk version is 1.3.x
html page:
<form enctype="multipart/form-data" action="/upload" method="post" >
<input type="file" name="myfile" />
<input type="submit" />
</form>
Server Code:
file_contents = self.request.POST.get('myfile').file.read()
If your still having a problem, check you are using enctype in the form tag
No:
<form encoding="multipart/form-data" action="/upload">
Yes:
<form enctype="multipart/form-data" action="/upload">
You can not store files as there is not a traditional file system. You can only store them in their own DataStore (in a field defined as a BlobProperty)
There is an example in the previous link:
class MyModel(db.Model):
blob = db.BlobProperty()
obj = MyModel()
obj.blob = db.Blob( file_contents )
Personally I found the tutorial described here useful when using the Java run time with GAE. For some reason, when I tried to upload a file using
<form action="/testservelet" method="get" enctype="multipart/form-data">
<div>
Myfile:<input type="file" name="file" size="50"/>
</div>
<div>
<input type="submit" value="Upload file">
</div>
</form>
I found that my HttpServlet class for some reason wouldn't accept the form with the 'enctype' attribute. Removing it works, however, this means I can't upload any files.
There's no flat file storing in Google App Engine. Everything has to go in to the Datastore which is a bit like a relational database but not quite.
You could store the files as TextProperty or BlobProperty attributes.
There is a 1MB limit on DataStore entries which may or may not be a problem.
I have observed some strange behavior when uploading files on App Engine. When you submit the following form:
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="img" />
...
</form>
And then you extract the img from the request like this:
img_contents = self.request.get('img')
The img_contents variable is a str() in Google Chrome, but it's unicode in Firefox. And as you now, the db.Blob() constructor takes a string and will throw an error if you pass in a unicode string.
Does anyone know how this can be fixed?
Also, what I find absolutely strange is that when I copy and paste the Guestbook application (with avatars), it works perfectly. I do everything exactly the same way in my code, but it just won't work. I'm very close to pulling my hair out.
There is a way of using flat file system( Atleast in usage perspective)
There is this Google App Engine Virtual FileSystem project. that is implemented with the help of datastore and memcache APIs to emulate an ordinary filesystem. Using this library you can use in you project a similar filesystem access(read and write).