Forums

Upload PDF and show in browser

Hi,

I'm not new to Python, but I'm just a hoobyst and I'm completely new to web dev, Flask and Python Anywhere.

I want to make a web app do deal with PDF files uploaded by users. My first goal is to simply let the user upload a PDF, then show it in the browser.

I made the code below, but I'm receiving the message below, what am I doing wrong?

Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

UPDATE: I've figured it. The code below is updated and working!

from flask import Flask, render_template, request

app = Flask(__name__)
app.config["DEBUG"] = True

BASE_DIR = os.path.dirname(__file__)
PUBLIC_ROOT = os.path.join(BASE_DIR, "public")

@app.route("/", methods=["GET", "POST"])
def main():
    if request.method == "POST":

        file = request.files["input_file"]

        if not file:
            return'''
        <html>
            <body>
                <p>NO FILE</p>
            </body>
        </html>
        '''

        filename = secure_filename(file.filename)
        file.save(os.path.join("/home/Saitodepaula/public/", filename))

    return'''
    <html>
        <body>
            <p>
                <a href={} target = "_blank">
                    <button type="button">Abrir</button>
                </a>
            </p>

            <p>
                <a href={} Download>
                    <button type="button">Download</button>
                </a>
            </p>

            <p>
                <a href="">
                    <button type="button">Send another file</button>
                </a>
            </p>

        </body>
    </html>
    '''.format("https://saitodepaula.pythonanywhere.com/public/" + filename, "https://saitodepaula.pythonanywhere.com/public/" + filename)

return '''
    <html>
        <body>
            <p>Select a file:</p>
            <form method="post" action="." enctype = multipart/form-data>
                <p><input type="file" name="input_file" accept=".pdf" ></p>
                <p><input type="submit" value="Process the file" /></p>
            </form>
        </body>
    </html>
'''

Where are you seeing this error? What can you see in your log files?

This error is in the browser, in my own website:

https://saitodepaula.pythonanywhere.com/

The error log doesn't show anything.

enter image description here

I found the file here, inside my main directory.

It looks pythonanywhere saves it here by default, but it doesn't know how to find it again if I don't specify the path.

So I think is a matter of how I specify where I want it to save the file right?

enter image description here

If it's saving the file, my guess is that you've moved on from the code that you posted earlier. Yes, you'll need to specify where it should be saved -- I would suggest that you create a directory called something like "uploads" in your home directory, and then save uploaded files there. Then you can set up a static file mapping with a "URL" set to "/uploads" and a directory set to "/home/Saitodepaula/uploads", and users will be able to view the uploaded files at URLs like https://saitodepaula.pythonanywhere.com/uploads/something.pdf.

If it's saving the file, my guess is that you've moved on from the code that you posted earlier.

I didn't. The code in the first post is the one on-line right now. And you can see in my last print the date showing for the uploaded file is today: I've uploaded the file just before posting this print screen here.

What I've understood till now is that, if not specified in the html code, pythonanywhere will just save the file there in the main folder. I thought that, if I don't make a code to save the file, it would not save the file in the server.

Anyway, I'll probably go with the solution you suggested.

Ok, thanks for letting us know

Ok, I've managed to save the uploaded PDF to a static folder I've mapped in the WEB tab, but I still can't get the link to this file.

I've updated the first post with the script, but I'm still getting the The requested URL was not found on the server. message.

I could only get the uploaded PDF to show in my browser pasting the complete path inside my html code:

https://www.pythonanywhere.com/user/Saitodepaula/files/home/Saitodepaula/mysite/static/Roteiro_para_video.pdf

But when I use

os.path.join(STATIC_ROOT, filename)

I only get /home/Saitodepaula/mysite/static/Roteiro_para_video.pdf, resulting in the URL not found message

I know I can get it to work with a code like

.format("https://www.pythonanywhere.com/user/Saitodepaula/files" + os.path.join(STATIC_ROOT, filename))

But this is not what the official tutorial makes me understand, so it gets confusing.

That URL is only visible to you when you are logged in to PythonAnywhere. If anyone else tried to access it, it would not work.

That URL is only visible to you when you are logged in to PythonAnywhere. If anyone else tried to access it, it would not work.

And how to make it accessible to anyone? For example, if I want to make an app that a user can upload a file, my app process it and the user can download it?

Update: I've figured it. The code in the first post is updated and working.

Glad to hear that!