A .tar.bz2 file served from web.py is saved as .tar.bz2 from the browser. However when served by flask, the .bz2 extension is removed (but running file against the file still identifies it as a .bz2 file).
I have both frameworks running (web.py on python 2.7 and flask on python 3.7).
I manually put the same .tar.bz2 file in a directory and serve it from the web page.
Web.py:
=======
<form method="get" action="$fName">
<div id="formsubmitbutton">
<input type="submit" name="Retrieve File" value="Retrieve File" class="button3" />
</div>
</form>
Flask:
======
<form method="get" action="{{ fName }}">
<div id="formsubmitbutton">
<input type="submit" name="Retrieve File" value="Retrieve File" class="button3" />
</div>
</form>
I would expect both setups to return fName.tar.bz2. Chrome is returning the following and I have no clue why, but suspect it has something to do with my problem:
"Resource interpreted as Document but transferred with MIME type application/x-tar"
UPDATE
Thanks for the tip. From Chrome though, that was the entire error message, minus the actual file name since there is customer information in there. Here is is edited error:
Resource interpreted as Document but transferred with MIME type application/x-tar: "http://foo.example.com:5000/static/1564445156.7369618/filename.tar.bz2?Retrieve+SSD=Retrieve+SSD".
Also, there is no traceback from web.py or Flask, because there is no error. it's just that when you click on retrieve file, web.py returns the full filename with .bz2 extenstion, and Flask returns the file without the .bz2 extension.
The .bz2 files I am trying to serve are in the /static directory. By adding an app.route('/static') function, I was able to serve the file as a .bz2 without it being stripped of the .bz2 suffix:
#main.route('/static')
def sendfile():
for key,val in request.values.items():
filedir = '/home/admin/Flask/httsTools'+os.path.dirname(key)
filename = os.path.basename(key)
return send_from_directory(filedir, filename, as_attachment=True)
Whether or not this is the recommended way to do it I'm not sure, but it works.
Thank you for your help.
Related
For some reason, this won't accept json files.
#app.route('/get_data', methods=['POST'])
def get_data():
dataFile = request.files['file_path']
dataFileName = dataFile.filename
dataFile.save(os.path.join(uploads_dir, dataFileName))
I keep getting this error:
You seem to have json set as an file ending in your template by <input type="file" accept="json">. (The template is not supplied so I can't pinpoint the line. This is not an error of the backend (flask) but of the your template code (jinja/html). It would be nice if you could supply a MRE for such issues.
For more information about <input type="file"> take a look at the MDN Documentation.
Example of correct accept:
<input type="file" accept=".json">
This will only allow *.json file but keep in mind that users may supply other files manually and create a fallback or validation when parsing/ saving the file.
I am trying to display the image on my local machine. I only use the website from my own machine. I am not expecting visit from outside. I found a solution here: Get Flask to show image not located in the static directory, But it doesn't work for me. I have tried:
relative path, abs path. None of them works. Where I did it wrong?
QUESTION:
for test purpose, my file system is like this:
C:/jackson/Python34_workspace/Python34_Projects/Learn-Bottle/app05_rend_local_img/
picuture_gallery/Desert.jpg
views/index.tpl
main.py
python script is this
#bottle.route("/")
def index():
return bottle.template("index.tpl")
#bottle.post('/result')
def result():
return bottle.template("index.tpl")
And this is my template.
<form action="/result" method="POST">
<br>
<input type="submit" value="Submit">
<br>
</form>
<div>
<img src="file:///C:/HSH/Python34_workspace/Python34_Projects/Learn-Bottle/app05_rend_local_img/picture_gallery/Desert.jpg">
</div>
--- Some comment ---
I have tried
src="file:///picuture_gallery/Desert.jpg"
after I clicked submit, it doesn't display. But if I drag it to the browser, it works. How could that be?
An URL using the file procotol is never requested from the server. The client (browser) always looks for it on the local system.
So it doesn't matter how you configure your Bottle application, the browser will not ask it for such an URL.
If you want the Botte application to deliver static files, do something like this:
from bottle import static_file
#route('/static/<filename>')
def server_static(filename):
return static_file(filename, root='/path/to/your/static/files')
This question already has answers here:
Displaying uploaded file in GAE Appengine with Python
(3 answers)
Closed 8 years ago.
I'm uploading a small pdf file to be stored as a blob in the Datastore.
Here's the uploading html, getting the PDF from the user:
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="pdf">
<input type="submit" value="Upload">
</form>
Here's the handler, storing the PDF to the Datastore:
def post(self):
p = self.request.POST['pdf']
if p:
person.pdf = p.value
Here's the view, showing the user the contents of the PDF:
<embed src="{{ person.pdf }}" width="500"
height="375" type="application/pdf">
According to all the information I have found, the content of the PDF should reside in p.value. However, the person.pdf attribute is None, and, of course, nothing is displayed.
The most basic way that this appears to be wrong is that:
<embed src="{{ person.pdf }}">
should contain the URL to download the pdf file. However, you're uploading a file via your upload form, and presumably storing the file data.
There's at least a few things that can go wrong, you should debug through and at the very least, isolate where something is going wrong:
Are you actually uploading the file?
How is the file encoded as it's uploaded?
How is the file decoded when you get it from the POST data?
How are you storing the actual file in person.pdf?
And finally, are you actually saving person after you modify it? It's generally more helpful to show all your code rather than snippets. And if that's all your code, where on earth is person coming from? It's not actually being initialized anywhere.
I would like that users of my ZOPE/Plone website can upload (big) file (>1Gb) on a server.
I have a form in html :
<form enctype="multipart/form-data" action="upload.py" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
I have an external script with ZOPE : upload.py
def get(self, REQUEST):
filename = REQUEST.file['file']
Unfortunately I don't know what to do with this file..
I found some tutorial but I think I'm on wrong way (because these methods can't work with ZOPE ?):
CGI : http://webpython.codepoint.net/cgi_file_upload
ftplib : Python Script Uploading files via FTP
Thanks for your advices,
It depends on how and where you want to store it.
The REQUEST.file is a file object where you can read, seek, tell etc the contents from.
You can store it like a blob:
from ZODB.blob import Blob
blob = Blob()
bfile = blob.open('w')
bfile.write(REQUEST.file)
bfile.close()
# save the blob somewhere now
context.myfile = blob
I'm trying to use Python and HTML together. What I'm trying to do is create an HTML Form that will submit data to a python file and that python file will then handle the data. But I'm not getting it to work.
Here is my python code:
form = cgi.FieldStorage() # instantiate only once!
name = form['Sample Name'].value
and this is my HTML code:
<form method='POST' action='/functionGen.py'>
Name: <input type='text' name='Sample Name'>
<input type='submit' value='Begin Storing'>
</form>
What ends up happening is I just see my python code in the browser, the file doesn't begin handling the data.
What can I do?
You should know, that you are getting plain python source document via http protocol now. If you want to use CGI mechanism, you should place youre .py in cgi-enabled directory. It means that you need http server.
related questions
How to run Python CGI script
How do I set up a Python CGI server?