uploading html files and using python - python

For my forum, I tried uploading a file with html using:
<form action="profiles.py" method="post">
<label class="labelOne" for="pics">Change your Profile pic </label>
<input type="file" name="uploadField" />
My python function takes that file, creates a file under userprofiles, and writes the data to it:
file = open('../data/accounts/userprofiles/'+str(form['name'].value)+'/'+'profilepics', 'w+')
file.write(str(form.getvalue('uploadField')))
file.close()
So, If I want burger.jpg to be my picture, I upload it, python takes that and creates a file with that name burger.jpg using w+ and then it writes the data to it(which should be the image).
However for some reason, I get no image.
What should I try next?

Set the enctype of your form to multipart/form-data
<form action="profiles.py" method="post" enctype="multipart/form-data">

Related

HTML/Python Upload and download a file

I have a button type ="file", which uploads a file. I'm using Google App Engine.
<form action="/sign?%s" method="post">
<div><br><br>Select a file: <input type="file" value="file"></div>
<form method="get" action="file">
<button type="submit">Download</button>
</form>
I have two questions:
Is it possible, after choosing a file to display more options (rather than just the file name), for example format and timestamp?
I would like to create a download button now, that downloads the file I have uploaded using the above upload button, is there a simple way to do that?

How to use correctly name variable in file upload form data? How to upload file with python script?

I can upload a file via a browser using this code:
<form method="POST" enctype="multipart/form-data" action="domen.com/file.php">
<input type="file" name="var_name">
<input type="submit" value="send">
</form>
But I can't upload a file with this script
values = {'name':'var_name','type':'filename'}
files = {'file': open('uploadfile.php', 'rb')}
res1 = requests.post('domen.com/file.php', files = files, data = values, timeout = timeouts)
Why?

Upload .xls file with html input file

I can successfully upload a .csv, and a .json file with the following code. However, when I try to upload a .xls files, the app gets locked up. What do I need to do to make this work correctly for Excel files?
class CreateTeam(BaseHandler):
def post(self):
file = self.request.get('file') # this line locks up with Excel files
My html looks like this...
<form method="POST" action="/create_team" enctype="multipart/form-data">
<div>
<p>Upload the file</p>
<input type="file" name="file">
</div>
</form>
I also have some other input fields on the form that are posted with the same submit button. Is this a MIME type issue. If so, how do I set the MIME type for just the uploaded file?

Creating an object with an external form

I have a upload form that is going to S3 --
<form action="https://test.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="id_file" />
<input type="submit" value="Upload to Amazon S3" name="upload">
</form>
When the form is submitted to S3, I also need to create an object in my db and get the id of the object. How would I do this (without redirecting to my view)? Thank you.
You could use an iframe to send the form and js to search for the id you are looking for in the retrieved page.
You could also use another script to perform the post method like php with the curl library. You could grab all the data retrieved from there, search for what you are looking for and add what you need to your database.

How to upload multiple files to BlobStore?

I'm trying to upload multiple files in a form to the BlobStore.
Form:
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data">
<label>Key Name</label><input type="text" name="key_name" size="50"><br/>
<label>name</label><input type="text" name="name" size="50"><br/>
<label>image</label><input type="file" name="image" size="50"><br/>
<label>thumb</label><input type="file" name="thumb" size="50"><br/>
<input type="submit" name="submit" value="Submit">
</form>
I'm then trying to fetch the BlobInfo objects for each of those files uploaded:
def post(self):
image_upload_files = self.get_uploads('image')
thumb_upload_files = self.get_uploads('thumb')
image_blob_info = image_upload_files[0]
thumb_blob_info = thumb_upload_files[0]
I'm seeing some weird behavior. Both files are making it into the BlobStore, but I cannot figure out how to get the Keys so that I can store those on another Entity. The code above manages to get the key for image_blob_info, but not thumb_blob_info. I don't understand how to use get_uploads. I want to pass multiple files through the form and then fetch them by name so I can store them in the appropriate BlobReferenceProperties on another Entity.
Each file needs its own unique upload url, so my guess is something wacky is happening when all three files are posted to the same url.
The best solution for supporting multiple file uploads is described in Nick Johnson's blog post:
http://blog.notdot.net/2010/04/Implementing-a-dropbox-service-with-the-Blobstore-API-part-3-Multiple-upload-support
You could post the files to the same name, followed by [], which will post an array:
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data">
<label>Key Name</label><input type="text" name="key_name" size="50"><br/>
<label>name</label><input type="text" name="files[]" size="50"><br/>
<label>image</label><input type="file" name="files[]" size="50"><br/>
<label>thumb</label><input type="file" name="thumb" size="50"><br/>
<input type="submit" name="submit" value="Submit">
</form>
Then in your form handler, you can something like this (depending on your web framework):
for uploaded_file in request.FILES.getlist('files'):
#do something with uploaded_file
Using the latest version of plupload, I was able to get the UploadQueue to work with GAE with this bit of code. Note, it is CoffeeScript, but should be easy to convert back to JavaScript if you really need to. It assumes you get a bit of json back from your server as {url:"gae generated url"}
$("#fileUploader").pluploadQueue
runtimes : 'html5,html4'
use_query_string : false
max_file_size : '3mb'
multipart: true
unique_names : true
multiple_queues : true
filters : [{title : "Image files", extensions : "jpg,gif,png"}]
preinit:
UploadFile: (up, file) ->
$.ajax
url: '/api/upload/url'
async: false
success: (data) ->
up.settings.url = data.url

Categories