chalie001
asked on
attach document in web page
how to attach document in flask i have this code
route('/download')
def download():
file = open('name.doc','r')
returnfile = file.read().encode('latin- 1')
file.close()
return Response(returnfile,
mimetype="text/doc",
headers={"Content-disposit ion":
"attachment; filename=name.doc"})
i what to attach the document
this my html i create the download folder
route('/download')
def download():
file = open('name.doc','r')
returnfile = file.read().encode('latin-
file.close()
return Response(returnfile,
mimetype="text/doc",
headers={"Content-disposit
"attachment; filename=name.doc"})
i what to attach the document
this my html i create the download folder
{% extends "layout.html" %}
{% block content %}
<div class="home">
<h1>My homepage</h1>
<p>Curriculium Vita</p>
</div>
{% endblock %}
ASKER
its not downloading
{% extends "layout.html" %}
{% block content %}
<div class="home">
<h1>My homepage</h1>
<p>Curriculium Vita</p>
<a href='/download'>Download</a>
<p><a href="C:/PythonProject/flask/mysite/MyApp/download/cv4.docx">Curriculium Vita</a></p>
</div>
{% endblock %}
href="C:/PythonProject/fla sk/mysite/ MyApp/down load/cv4.d ocx"
This should be realitve to your site, not the hard drive. Or the full url like mysite. com/download/cv4.docx.
also make sure your server allows docx file types.
This should be realitve to your site, not the hard drive. Or the full url like mysite. com/download/cv4.docx.
also make sure your server allows docx file types.
ASKER
am geting this error
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
Try this before you do anything else.
1) Create a sample page using the code below and name it test.html
2) Upload test.html file to your live server top level. You should be able to surf to http://yourdomain.com/test.html.
If you have steps 1 and 2 working, go to step 3.
3) Create a top level folder called "downloads" and move it to your live server.
4) Create a new text file called file.txt and upload it to your downloads folder.
5) Test that the file exists by surfing to http://yourdomain.com/downloads/file.txt
If you are able to complete step 5, please try going to http://yourdomain.com/test.html and try the link. If it is not working, please post the domain you are practicing on so we may try it and help find the error.
1) Create a sample page using the code below and name it test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>test</title>
</head>
<body>
<a href="/downloads/file.txt">link to file.txt</a>
</body>
</html>
2) Upload test.html file to your live server top level. You should be able to surf to http://yourdomain.com/test.html.
If you have steps 1 and 2 working, go to step 3.
3) Create a top level folder called "downloads" and move it to your live server.
4) Create a new text file called file.txt and upload it to your downloads folder.
5) Test that the file exists by surfing to http://yourdomain.com/downloads/file.txt
If you are able to complete step 5, please try going to http://yourdomain.com/test.html and try the link. If it is not working, please post the domain you are practicing on so we may try it and help find the error.
Here is an example file using a full url instead of a link to a site
https://jsbin.com/lovuzayixo/edit?html,output
https://jsbin.com/lovuzayixo/edit?html,output
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>test</title>
</head>
<body>
<a href="https://filedb.experts-exchange.com/incoming/2019/11_w47/1436914/file.txt">link to file.txt</a>
</body>
</html>
ASKER
i add the test page but is not showing when i run
from flask import Flask, render_template,send_file
app=Flask(__name__)
@app.route('/')
def home():
return render_template("home.html")
@app.route('/about/')
def about():
return render_template("about.html")
@app.route('/test/')
def test():
return render_template('test.html')
if __name__=="__main__":
app.run(debug=True)
@app.route('/download/')
def download():
try:
#return send_file('C:/PythonProject/flask/mysite/MyApp/download/cv4.doc', attachment_filename='cv4.doc')
#return send_file(r'C:/PythonProject/flask/mysite/MyApp/download/cv4.doc', attachment_filename='cv4.doc')
return send_file(r'C:\PythonProject\flask\mysite\MyApp\download\cv4.doc',
attachment_filename='/download/cv4.doc')
except Exception as e:
return str(e)
You are showing python code and at this point you have no idea if the issue is your html or the python code that generates the html.
Take it in steps. The first step is to manually create your html page, upload it and get that to work first. Find any errors in the html, fix and run again. You will end up with something like https://www.experts-exchange.com/questions/29164415/attach-document-in-web-page.html?anchorAnswerId=42980863#a42980863
Now that you have your html set, you can run your python code that generates the html. If something is not working, you already know that the html is good, leaving an error in how you are generating your html from python.
Looking at your code, and I am not a python dev, it does appear you didn't change anything. Go back to your issue here https://www.experts-exchange.com/questions/29164415/attach-document-in-web-page.html?anchorAnswerId=42980475#a42980475. We talked about why this
Take it in steps. The first step is to manually create your html page, upload it and get that to work first. Find any errors in the html, fix and run again. You will end up with something like https://www.experts-exchange.com/questions/29164415/attach-document-in-web-page.html?anchorAnswerId=42980863#a42980863
Now that you have your html set, you can run your python code that generates the html. If something is not working, you already know that the html is good, leaving an error in how you are generating your html from python.
Looking at your code, and I am not a python dev, it does appear you didn't change anything. Go back to your issue here https://www.experts-exchange.com/questions/29164415/attach-document-in-web-page.html?anchorAnswerId=42980475#a42980475. We talked about why this
<a href="C:/PythonProject/flask/mysite/MyApp/download/cv4.docx">Curriculium Vita</a>
is wrong and what it should be.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window