Link to home
Start Free TrialLog in
Avatar of chalie001
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-disposition":
                 "attachment; filename=name.doc"})

i what to attach the document
User generated image
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 %}

Open in new window

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

You need an 'a' link that encloses your text https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a

<p><a href="/path_to_file/file.pdf">Curriculium Vita</a></p>

Open in new window

Avatar of chalie001
chalie001

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 %}

Open in new window

href="C:/PythonProject/flask/mysite/MyApp/download/cv4.docx"


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.
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.
Try this before you do anything else.

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>

Open in new window


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
<!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>

Open in new window

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)

Open in new window

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
<a href="C:/PythonProject/flask/mysite/MyApp/download/cv4.docx">Curriculium Vita</a>

Open in new window

is wrong and what it should be.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members 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.