Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

upload file using ajax

// add jquery
// <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
$(document).ready(function() {
  $("#form").on('submit', (function(e) {
    e.preventDefault();
    $.ajax({
      url: $(this).attr('action'),
      type: "POST",
      data: new FormData(this),
      contentType: false,
      cache: false,
      processData: false,
      success: function(response) {
        $("#form").trigger("reset"); // to reset form input fields
      },
      error: function(e) {
        console.log(e);
      }
    });
  }));
});
Comment

ajax file upload input

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>File Uploads With AJAX</title>
</head>
<body>
    <form id="file-form" action="fileUpload.php" method="post" enctype="multipart/form-data">
        Upload a File:
        <input type="file" id="myfile" name="myfile">
        <input type="submit" id="submit" name="submit" value="Upload File Now" >
    </form>

    <p id="status"></p>
    <script type="text/javascript" src="fileUpload.js"></script>
</body>
</html>

Code language: HTML, XML (xml)
Comment

ajax file upload

$(function() {
    // Configure Cloudinary
    // with the credentials on
    // your Cloudinary dashboard
    $.cloudinary.config({ cloud_name: 'YOUR_CLOUD_NAME', api_key: 'YOUR_API_KEY'});
    // Upload button
    var uploadButton = $('#submit');
    // Upload-button event
    uploadButton.on('click', function(e){
        // Initiate upload
        cloudinary.openUploadWidget({ cloud_name: 'YOUR_CLOUD_NAME', upload_preset: 'YOUR_UPLOAD_PRESET', tags: ['cgal']}, 
        function(error, result) { 
            if(error) console.log(error);
            // If NO error, log image data to console
            var id = result[0].public_id;
            console.log(processImage(id));
        });
    });
})
function processImage(id) {
    var options = {
        client_hints: true,
    };
    return '<img src="'+ $.cloudinary.url(id, options) +'" style="width: 100%; height: auto"/>';
}
Code language: JavaScript (javascript)
Comment

PREVIOUS NEXT
Code Example
Javascript :: function for flatten an array 
Javascript :: react recursive component 
Javascript :: create bottom navigation bar react native 
Javascript :: encode password javascript 
Javascript :: loop an array javascript 
Javascript :: check if browser supports Local Storage 
Javascript :: how to check empty object js 
Javascript :: stack example in javascript 
Javascript :: random trong js 
Javascript :: swap scroll right in react native 
Javascript :: get channel object using its name discod.js 
Javascript :: settimeout with loop js 
Javascript :: Factorial Recursion Function in Javascript 
Javascript :: vue implode array 
Javascript :: javascript training 
Javascript :: how to split an array into two javascript 
Javascript :: redux saga use navigation 
Javascript :: crypto in node js 
Javascript :: model export in node js 
Javascript :: monaco editor get content 
Javascript :: date to string format javascript 
Javascript :: how to cast in javascript 
Javascript :: counter in html and js 
Javascript :: react select error handle 
Javascript :: Slice and Splice -Javascript 
Javascript :: date range query knex 
Javascript :: on window resize js 
Javascript :: javascript less than but greater than 
Javascript :: nds npm 
Javascript :: react-google-login 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =