R
R
rusgayfer2018-02-17 15:00:36
JavaScript
rusgayfer, 2018-02-17 15:00:36

How to send images via Ajax?

The image block is

<span class="btn btn-primary fileinput-button">
                    <i class="fa fa-fw fa-camera"></i>
                     <input type="file" name="files[]" multiple="" accept="image/jpeg, image/png, image/gif" id="basicUploadFile">
                    </span>

This is how it doesn't work
'files': $("#basicUploadFile").val(),
$("#confirm").on('click', function() {

    var data = {
        'message': $("#message").val(),
        'ques': $("#ques").val(),
        'name_music': $("#who_music").val(),
        'name_film': $("#who_film").val(),
        'name_doc': $("#who_doc").val(),
        'files': $("#basicUploadFile").val(),
        'token': $("#token").val(),
        'name': $("#name").val(),
        'search_music': $("#search_result_mu").val(),
        'video_chekboks': $("#video_chekboks").val(),
        'doc_chekboks': $("#doc_chekboks").val(),
        'id_cook': $("#id_cook").val(),
        'g-recaptcha-response': $("#g-recaptcha-response").val(),
        'captcha_post': $("#captcha_post").val(),
        'captcha_sid': $("#captcha_sid").val(),
    };		
    
    $.ajax({
      url:"/api_asked", 
      data:data, 
      type:'POST', 
      dataType: 'json',
      success:function(data){
        if( data.code == 1 ) {
          $('#okey').html(data.message);
        }
        if( data.code == 'error' ) {
          $('#okey').html(data.message);
        }
      }
    });

  });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Tsvetkov, 2018-02-17
@yellow79

Use FormData

N
Nikita Kit, 2018-02-17
@ShadowOfCasper

To send images to the server, you can come up with 2 things - 1 decompose the image into a binary (base64 - I didn’t do this, but I know that it’s possible - google it). 2 - a more standard approach - the new formData()
object was specially invented for this . It will come to the server - an ordinary string
Working example:
https://github.com/WebKieth/Black-UI/blob/master/s...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question