Upload .xls file with html input file - python

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?

Related

In Django I want to fetch the selected file name file is in my media folder so I want to fetch it and display it in html that the file is selected

This is my html code for update the form I have the uploaded file through a html form and the pdf file is getting stored in my media folder and the file name in database, so like the text fields I also want to retrieve the selected file from the media folder the value ="{{i.cv}}" is not working like I did it in text fields so basically I have two html one is for
filling up where there is also file field am uploading pdf files in it and it is storing in media folder another html for updating the data in there the upload cv does not select any file what's the solution for this.
this is my html
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black">Upload CV</label></br>
<input type="file" name="cv" id="cv1" value ="{{i.cv}}">
</div>
</div>
here is models.py
class ApplyForm(models.Model):
cv = models.FileField(upload_to='')
this is views.py
def update(request, id):
i.cv = request.FILES['cv']
I just want the file to be selected during edit I am uploading pdf files

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?

Django: Uploading an XML file from HTML page

I am using Django framework and python scripts for validating XML files.
I usually parse the XML file, which is already present in the defined location, using the below code.
import xml.etree.ElementTree as ET
tree = ET.parse('config.xml')
However, now I want to add the file dynamically from the front end browse file option and place it in the above ET.parse('file.xml') location. What is the best way to achieve this?
<form action="/handle_xml_upload/" enctype="multipart/form-data" method="post">
{% csrf_token %}
<input type="file" name="xmlfile">
<input type="submit" value="upload xml file">
</form>
and in views.py
def handle_xml_upload(request):
xmlfile = request.FILES['xmlfile']
tree = ET.parse(xmlfile)
# ...
of course, you need adjust your urls.py as well ;)

Trying to use pandas and xlsx writer in GAE

So I have created a basic local page on GAE which has a header, a field, and a submit button. My end goal is to create a local page which will take an input of a dataframe from pandas in the field, and convert that to an excel file when I hit the submit button. I have a program written in python already to do this, but I can't figure out how to transfer that over. Here is my code for the page I have already:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
myPage = """
<html>
<body>
<form action="/hello" method="get">
<div>
<input type="text" name="yourname" size="30" maxlength="30"></input>
</div>
<div>
<input type="submit" value="Submit dataframe">
</div>
</form>
</body>
</html>
"""
self.response.out.write(myPage)
class hello(webapp2.RequestHandler):
def get(self):
yourname = self.request.get('yourname')
self.response.out.write(yourname + " to you too")
application = webapp2.WSGIApplication([
('/', MainPage),('/hello', hello)], debug=True)
When I try to import xlsxwriter or pandas my page won't load. I tried to update my yaml file, but to no avail. I really need to figure out how to do this. It seems like it should be fairly easy, but I don't know where to go from here. In my mind I should be able to just:
import xlsxwriter, pandas, os, re
copy and paste in my other code which only uses those things
make the call: custom_tabs(yourname), which will take in the dataframe and spit out the excel doc.

uploading html files and using 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">

Categories