How can I get address of file in bottlepy?
I tried it, but it returns %s.shp
#get('/upload')
def upload_form():
return '''
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="filer" /><br />
<input type="submit" />
</form>
'''
#post('/upload')
def upload_submit():
file_csv = request.get("filer") # I want to get file address (string)
map = inflation_map.InflationMap(file_csv)
map.draw_image()
return file_csv
When you do :
request.get("filer")
you will just get the name of the file. You need to save it on to the file system by specifying a path.
It is documented here : http://bottlepy.org/docs/dev/tutorial.html#file-uploads
Related
compress.html
<form action="/compress" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label for="pdf">Select pdf :</label>
<input type="file" name="pdf1" id ="pdf1" accept="pdf/*" />
<br>
<button type="submit" class="btn btn-dark mt-2" value="click">
submit
</button>
<script type="text/javascript">
function getFilePath(){
$('input[type=file]').change(function () {
var filePath=$('#fileUpload').val();
});
}
</script>
views.py
def mergepdf(request):
from pylovepdf.ilovepdf import ILovePdf
ilovepdf = ILovePdf('my_secrete_key', verify_ssl=True)
task = ilovepdf.new_task('compress')
task.request.FILES['full_path']# here i want full path of selected file
task.set_output_folder('/Downloads/download_pdffile')
task.execute()
task.download()
task.delete_current_task()
The filePath var contains the only name of selected file, not the full path.
I searched it on the net, but it seems that for security reasons browsers (FF, chrome) just give the name of the file.
Is there any other way to get the full path of the selected file?
Getting a 400, when trying to upload a file ad send other form elements to flask from html. Tried to use ajax, but that throws me an error as well.
Python:
#app.route('/prod_diff_result', methods=['POST', 'GET'])
def prod_diff_result():
try:
host = request.form['host-prod-iterator']
print(host)
if request.files['file']:
f = request.files['file']
f.save(secure_filename(f.filename))
HTML:
<div class="main-div">
<form action="/prod_diff_result" method="POST" enctype="multipart/form-data">
<div class="grid-container">
<div class="grid-item">
<span class="label label-default ">PROD</span><br>
<p>Iterator Host : <input type="text" class="form-control" id="host-prod-iterator" value="10.47.7.57"
required></p>
<input type="radio" name="data_fetch_type" value="file" onclick="showfile()">Upload File
<input type="file" name="file" />
<input type="radio" name="data_fetch_type" value="db"> Get from DB
<input type="submit" />
</div>
</form>
</div>
I want to be able send hostname and file back to flask error in one request and using one form.
It gives an error because you try to access a form field that it cannot find, and assumes that somehow the request was bad, because it didn't include a required form field. You are trying to access:
host = request.form['host-prod-iterator']
However you have simply not given it a name in your HTML. If you give it a name, it should work:
<p>Iterator Host :
<input type="text" class="form-control" name="host-prod-iterator" id="host-prod-iterator" value="10.47.7.57" required>
</p>
Trying to get some values from a form but the parameters are always empty. here is the path from my urls.py:
url((r'^partners-email$'), views.partners_email, name="Partners Email"),
This is a simple form I have in the template:
<form method="POST" action="/partners-email">
<input name="email" class="form-control" id="client-email">
<input type="submit" value="Submit" />
</form>
and here is my function in views.py:
def partners_email(request):
from_email = request.POST.get('email', '')
print('MY_TAG: ' + from_email)
output is always:
"MYTAG: "
any ideas?
Thank you very much in advance
Your input elements don't have a name attribute so the browser will never send data for them.
Try it.
<input type="text" name="email" class="form-control" id="client-email">
scriptInfo.py
import os, sys, platform, webbrowser
def main()
template = open('scriptHmtl.phtml').read()
scriptHtml.phtml
<html>
<head>
</head>
<body>
<h2><center> welcome </center></h2>
<br/><br/><br/>
...
variables
..
<form name="sendData" method="get" action="http://localhost:8000/cgi/scriptGet.py">
Name: <input type="text" name="n"><br/><br/>
First Name: <input type="text" name="fn"/><br/><br/>
Mail: <input type="text" name="ma"/><br/><br/>
Address: <input type="text" name="add"/> <br/><br/>
<input type="submit" value="OK"/>
</form>
Instead of action="http://localhost:8000/cgi/scriptGet.py", there must be a variable which contain the code to recover the server address, but I don't want how to do it.
With HTML forms you can just ignore the server and go straight to the script.
For example like
<form name="sendData" method="get" action="cgi/scriptGet.py">
Trying to upload multiple files at once using python. The upload.html source code is as followed:
<form name="frmRegister" method="post" accept-charset="utf-8" enctype="multipart/form-data" class="form-horizontal">
<div class="control-group">
<div class="controls">
<input type="file" name="files" multiple='multiple'>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" name="btnSubmit" value="Add Product" />
</div>
</div>
</form>
in my admin.py:
#view_config(context="mycart:resources.Product", name="add", renderer='admin/mall/product/add.jinja2', permission = 'admin')
#view_config(context="mycart:resources.Product", name="add", request_method="POST", renderer='admin/mall/product/add.jinja2', permission = 'admin')
def product_add(context, request):
if 'btnSubmit' in request.POST:
print ("files >>> ", request.POST['files'])
in my terminal, it is showing just FieldStorage('files', u'DSC01973.JPG') whereas I've selected 'DSC01975.JPG', 'DSC01976.JPG'.
Why is this so?
I've found a way to solve it, I believe there are many others, if there are, please feel free to holler out:
fileslist = request.POST.getall('files')
print ("My files listing: ", fileslist)
for f in fileslist:
print ( "individual files: ", f )
I could solve the problem with the following function:
from cgi import FieldStorage
def get_all_file_data_list(request):
return [x for x in request.POST.values() if isinstance(x, FieldStorage)]