2

I have a simple python file that sends a file from a local directory to be displayed in html. And, when the user clicks submit, I want to save this file to a different directory, but I can't seem to make this happen. Here's my code:

Uploader.py

from __future__ import print_function
from random import choice
from flask import Flask, request, redirect, url_for, flash, render_template, abort, send_file, session
from werkzeug.utils import secure_filename
from flask import send_from_directory
import sys, os

app = Flask(__name__)
@app.route('/')
def init(file_Idx=0):
    files = os.listdir(DOWNLOAD_FOLDER)
    filePath = os.path.join(DOWNLOAD_FOLDER, files[file_Idx])
    return render_template('files.html', file=filePath)

@app.route('/upload', methods=['POST'])
def upload_file():
    file = request.files['image'] #Failing here!!!
    f = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
    file.save(f)
    return render_template('files.html')

files.html

<form action="/upload" method="post" enctype="multipart/form-data">
        <img src="{{file}}"/>                
        <input type=submit name="image">
</form>

Right now the image is displaying, but I can't seem to pass the file to upload_file() to save it in the upload_folder. How can I make this work?

yalpsid eman
  • 3,064
  • 6
  • 45
  • 71

1 Answers1

1

Try this, you can define your desired path in it. You can Edit the line

file.save(os.path.join("/tmp/", filename))

and place your desired path in it.

from flask import Flask, render_template, request
from werkzeug import secure_filename
app = Flask(__name__)

@app.route('/upload')
def upload_file():
    return render_template('upload.html')

@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
       file = request.files['file']
       if file and allowed_file(file.filename):
           filename = secure_filename(file.filename)
           file.save(os.path.join("/tmp/", filename))

if __name__ == '__main__':
    app.run(debug = True)

and HTML code for that

<form id="package_form" action="" method="POST">
<div>
   <p>Upload Packages:</p>
   <p><input id="upload_button" type="file" class="btn btn-default btn-xs" name="file"></p>
   <p><input id="submit_button" type="submit" class="btn btn-success" value="Upload">
</div>

Saad
  • 916
  • 1
  • 15
  • 28
  • This doesn't work. Furthermore, I don't want a choose file button for someone to choose a file. I want a file from a directory to be displayed and a button that, when clicked, saves the file to a different location – yalpsid eman Jun 20 '18 at 16:09
  • if you could highlight the specific error ? is there any error the application is showing while execution ? – Saad Jun 20 '18 at 16:10
  • "AssertionError: View function mapping is overwriting an existing endpoint function: upload_file" – yalpsid eman Jun 20 '18 at 16:11
  • see this "https://stackoverflow.com/questions/44926465/upload-image-in-flask" , may be you can find some help from the answers given here ! – Saad Jun 20 '18 at 16:14