0

With a form and the following input tag:

<input type="file" name="images" multiple="" class="inputFileHidden">

I can get the file images on my server just by simply searching through the request.files object:

images = request.files.getlist('images') // returns list of imgs.

In my case, I'm not working with a form, but rather I post it using AJAX.

With simple things like text I can easily post it as such:

Client side

    const i_post_title = document.getElementsByName('post_title')[0];
 
    ....
    
    $.post('/_upload_post', {
        title_post: i_post_title.value
    }).done(function(data){
        console.log(data)
    }).fail(function(){
        console.log('fail')
    })

And receive it on my server by accessing the request.form object and passing in the key of the payload sent from the client:

Server Side

title_post = request.form['title_post']

But with files, how should I handle it without having a form?

netlemon
  • 964
  • 8
  • 22
  • Are you looking for that? https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request.args – szatkus Jul 06 '20 at 15:51
  • If there is no form, where is the file data coming from? – TheSinisterShadow Jul 06 '20 at 18:54
  • Does this answer your question? [jQuery Ajax File Upload](https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload) – Musa Jul 06 '20 at 19:26
  • @keanu please rethink your question, the file data is coming from the html input tag `an input field where the user can enter data.` – netlemon Jul 07 '20 at 05:58
  • @Musa that doesn't answer my question. I've fixed the problem though, using `FormData` and some tweaking when handling `File` js objects. – netlemon Jul 07 '20 at 05:59

0 Answers0