image upload with ajax and php

后端 未结 4 1137
抹茶落季
抹茶落季 2020-12-30 17:01

I want to use ajax to upload image. In this module:

  1. On clicking browse and selecting image, it will be uploaded and displayed over file field.
4条回答
  •  天命终不由人
    2020-12-30 17:50

    Actualy you can upload images with the ajax function in Jquery in atleast the lates version of chrome.

    HTML:

    JS:

    $("form").submit(function(){
        var formData = new FormData($(this)[0]);
        $.ajax({
           url: window.location.pathname,
           type: 'POST',
           data: formData,
           async: false,
           cache: false,
           contentType: false,
           processData: false,
           success: function (data) {
                  alert(data);
           }
        }); 
        return false;
    }); 
    

    This script will send a post request with the created file data to the current page through Ajax. You can change the destination obviously through changing the url parameter.

提交回复
热议问题