0

I saw that one already, but I think i have error somewhere in code.

This is my form which in index.html

<form action="http://localhost:5000/uploader" method="POST" 
  enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" />
</form>

And this is flask function:

@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
   if request.method == 'POST':
      f = request.files['file']
      # f.save(f.filename)
      return 'file uploaded successfully'

Doesnt work at all... Any good tutorials or your answers more than welcome.

davidism
  • 121,510
  • 29
  • 395
  • 339
Lalabuy
  • 65
  • 1
  • 9

1 Answers1

0

This simplified code works in my app.

import tempfile
tempdirectory = tempfile.gettempdir()

class UploadView(Roled, BaseView):
if request.method == 'POST':
    if request.form['action'] == 'Upload':
        file = request.files['newfile']
        filename = secure_filename(file.filename)
        file.save(os.path.join(tempdirectory, filename))

and in the template:

<form action="" method="POST" enctype="multipart/form-data">
     <div>
       <input type="file" name="file"/>
     </div>
    <input class="btn" type="submit" name="action" value="Upload">
</form>
gittert
  • 1,238
  • 2
  • 7
  • 15
  • 1
    Isn't this still saving the file to the filesystem (in OS tmp dir)? – Laksitha Ranasingha Jun 21 '20 at 20:49
  • Indeed the file is temparary stored in temp folder. – gittert Jun 22 '20 at 05:38
  • 1
    So the OP is "Flask upload file without saving it" but your answer stores the file in tmp dir. So I don't think this is a valid answer. I am not sure you could do a file upload without saving. – Laksitha Ranasingha Jun 22 '20 at 17:55
  • 1
    Well, in some way you are right of course. However, the example code in OP is not finished as the f.save method is commented out, but the url show file upload in permanent storage location. The temp folder location is managed by the OS, and is not considered as permanent storage. I'm not aware of any method without using some kind of saving. Besides that, I'm not sure if there is a valid use case to not even use temp folder. – gittert Jun 22 '20 at 18:33