Kamis, 01 Oktober 2015

Create file upload form ajax

Create file upload form ajax - This tutorial is made by combining two different sources into one apikasi that can be used on your website so that it can have interesting applications simple file upload. NEW 2015
Create file upload form ajax
This tutorial comes from jQuery File Upload plugin combined with jQuery Knob. So it can generate upload application files are simple and easy to use is also very interesting of course.
To create a file upload form ajax, first we have to do is to create a script HTML5 standard.
<!DOCTYPE html>
<html>

 <head>
  <meta charset="utf-8"/>
  <title>Mini Ajax File Upload Form</title>

  <!-- Google web fonts -->
  <link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" rel='stylesheet' />

  <!-- The main CSS file -->
  <link href="assets/css/style.css" rel="stylesheet" />
 </head>

 <body>

  <form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
   <div id="drop">
    Drop Here

    <a>Browse</a>
    <input type="file" name="upl" multiple />
   </div>

   <ul>
    <!-- The file uploads will be shown here -->
   </ul>

  </form>

  <!-- JavaScript Includes -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script src="assets/js/jquery.knob.js"></script>

  <!-- jQuery File Upload Dependencies -->
  <script src="assets/js/jquery.ui.widget.js"></script>
  <script src="assets/js/jquery.iframe-transport.js"></script>
  <script src="assets/js/jquery.fileupload.js"></script>

  <!-- Our main JS file -->
  <script src="assets/js/script.js"></script>

 </body>
</html>
In the head of the document, I have included two fonts from Google Webfonts, and before the closing </body>tag you can see a number of JavaScript libraries. These are the jQuery library, the jQuery Knob plugin and the dependencies for the jQuery File Upload plugin.
The main element on the page is the #upload form. Inside it is the #drop div (which accepts drag/drop uploads) and an unordered list. This list will hold a li item for each of the transferred files. You can see the markup generated for a file upload below:
<li class="working">
 <input type="text" value="0" data-width="48" data-height="48" data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" />
 <p>Sunset.jpg <i>145 KB</i></p>
 <span></span>
</li>
Then you can also includes CSS script liking and of course you like. So look ajax file upload form more appear attractive. The default behavior of the plugin is to place the files in a queue, but we will make the files upload automatically when they are dropped / selected, which will make the experience more straightforward. You can see the JS below:
$(function(){

    var ul = $('#upload ul');

    $('#drop a').click(function(){
        // Simulate a click on the file input button
        // to show the file browser dialog
        $(this).parent().find('input').click();
    });

    // Initialize the jQuery File Upload plugin
    $('#upload').fileupload({

        // This element will accept file drag/drop uploading
        dropZone: $('#drop'),

        // This function is called when a file is added to the queue;
        // either via the browse button, or via drag/drop:
        add: function (e, data) {

            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
                ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');

            // Append the file name and file size
            tpl.find('p').text(data.files[0].name)
                         .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

            // Add the HTML to the UL element
            data.context = tpl.appendTo(ul);

            // Initialize the knob plugin
            tpl.find('input').knob();

            // Listen for clicks on the cancel icon
            tpl.find('span').click(function(){

                if(tpl.hasClass('working')){
                    jqXHR.abort();
                }

                tpl.fadeOut(function(){
                    tpl.remove();
                });

            });

            // Automatically upload the file once it is added to the queue
            var jqXHR = data.submit();
        },

        progress: function(e, data){

            // Calculate the completion percentage of the upload
            var progress = parseInt(data.loaded / data.total * 100, 10);

            // Update the hidden input field and trigger a change
            // so that the jQuery knob plugin knows to update the dial
            data.context.find('input').val(progress).change();

            if(progress == 100){
                data.context.removeClass('working');
            }
        },

        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        }

    });

    // Prevent the default action when a file is dropped on the window
    $(document).on('drop dragover', function (e) {
        e.preventDefault();
    });

    // Helper function that formats the file sizes
    function formatFileSize(bytes) {
        if (typeof bytes !== 'number') {
            return '';
        }

        if (bytes >= 1000000000) {
            return (bytes / 1000000000).toFixed(2) + ' GB';
        }

        if (bytes >= 1000000) {
            return (bytes / 1000000).toFixed(2) + ' MB';
        }

        return (bytes / 1000).toFixed(2) + ' KB';
    }

});
On review in making ajax simple file upload at this time also comes with a PHP file. It also will you save on your server to be able to run this simple application. So let us look at the following php script.
<?php

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

 $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

 if(!in_array(strtolower($extension), $allowed)){
  echo '{"status":"error"}';
  exit;
 }

 if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
  echo '{"status":"success"}';
  exit;
 }
}

echo '{"status":"error"}';
exit;
Until there first tutorial that I wrote in my article on this time. I hope this article you can take benefits. And do not forget I say thank you for your visit.
Keyword Tool : html file upload form, Create file upload form ajax php, php file upload form, laravel file upload form, wordpress file upload form, bootstrap file upload form, django file upload form, file upload form ajax, file upload form example, Create file upload form ajax.

Create file upload form ajax Rating: 4.5 Diposkan Oleh: Kang Icung

0 komentar:

Posting Komentar