Upload files using Ajax with Progress Bar

Uploading files using some server side languages are not a big deal. But, if we handle file uploads asynchronously, then we will be able to save time as well as some sudden overload to the server. Here is a very short working demo of code that uploads the file through Ajax. Actually, the file is uploaded via the server side code (here PHP), but the user don't have to step upto the next page and it feels like it is happening in client side.


  • 1.   Create an HTML file say, form.html and put the following code.

        <!DOCTYPE html>
        <html>
        <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
        <script src="http://malsup.github.com/jquery.form.js"></script>
        </head>
        <body>
        <div class="container">
        <h3 class="page-header">File Upload via Ajax with Progress Bar</h3>
        <div>
        <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="myfile"><br/>
        <input type="submit" value="Upload File to Server" class="btn btn-primary">
        </form>
        </div>
        <br/>
        <div class="progress">
        <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
        </div>
        <div class="percent">0%</div >
        </div>
        <p class="alert-info" id="status"></p>
        </div>
    
        <script>
        (function() {
        var bar = $('.progress-bar');
        var percent = $('.percent');
        var status = $('#status');
    
        $('form').ajaxForm({
        beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal);
        percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal);
        percent.html(percentVal);
        },
        success: function() {
        var percentVal = '100%';
        bar.width(percentVal);
        percent.html(percentVal);
        },
        complete: function(xhr) {
        status.html(xhr.responseText);
        status.addClass('alert');
        }
        });
    
        })();
        </script>
        </body>
        </html>

    2.  Now, handle the file upload into another file, upload.php as follows.
    <?php
        $uploaddir = 'E:/WebArtisans/';  //Path to a directory on your file system
        $uploadfile = $uploaddir . basename($_FILES['myfile']['name']);
    
        if (move_uploaded_file($_FILES['myfile']['tmp_name'], $uploadfile)) {
            echo "File is successfully uploaded.";
        } else {
            echo "Upload failed.";
        }
    ?>
    


    You can download the complete project from GitHub. Click here.

    Now, run and see. :)

    Windows 10 Release Date: July 29, 2015. How to Upgrade?

    Microsoft has announced the release date of its most awaited Operating System, Windows 10. Microsoft has announced through its official blog that it will be released on July 29 this year.
    People running Windows 7 and Windows 8/8.1 on their machines can upgrade to Windows 10 for free. In this version of Windows, it has the start button like in Windows 8.1, but is more user friendly and attractive.


    Features of Windows 10
    Windows 10 has many new and amazing features integrated. Some of them are:

    1. It has Cortana Personal Digital Assistant, a digital personal assistant for your daily work.
    2. It has Bio-metric System. You can unlock your device through your voice command, iris, finger-print and even from your face capture.
    3. Spartan Browser: an entirely new internet browser is associated with Windows 10. It still has Internet Explorer for legacy system compatibility.
    4. It has many other features such as Virtual desktop, Metro apps, Action Center Notifications, Revamped Mail and Calendar apps, Xbox apps etc.

    How to Upgrade?

    If you have Windows 7 or Windows 8/8.1 on your machine, then you can freely upgrade your machine to Windows 10. These are the steps you should follow:

    1. At first make sure that your Windows is having regular updates. If you regularly update you Windows, you should have already be seen a Windows icon in your notification tray of taskbar.


         2.  A window appears, click on the "Reserve Your Free Upgrade".



         3.  Enter you email address in the next window, accept the privacy statement and click on "Send Confirmation" button.


    4. All Done. You will get the Windows 10 on July 29 on your regular Windows Update. You won't miss a minute of its release. Almost 3GB files should be downloaded, that's only the time you require.


    Upgrade to the latest Operating System of Windows and enjoy. Remember, this is the last Operation System of Windows series. So, don't miss it.

    How to change the colors of folders?

    Creating Folders Having Different Colors In Windows:


    Windows doesn't allow us to create the colored folders by default. Hence we have to install a folder colorizer in our Windows to make it happen.


    1. Download the folder colorizer setup file from here.

    2. Install the downloaded setup file. If it asks to install the Microsoft .Net Framework, then install that too.

    3. After the successful installation, you can now proceed to add colors to the folders of you PC.

    4. Right click any folder to which you want to add color.

    5. Select "Colorize!" option and then select the color you want to add.


    6. Now refresh (F5) your computer to see the instant effect.

    Enjoy! :)

    Like us on Facebook.
    Web Artisans

    Program to find the sum of the series 0.9, 0.99, 0.999, 0.9999, ..........

    Problem:

    WAP to display the series 0.9, 0.99, 0.999, 0.9999, .......... and also find the sum of the series.

    Source Code:

    /*to find the sum of the series 0.9, 0.99, 0.999, 0.9999, ..........*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        int i, n;
        double j=0, sum=0;
        printf("Enter the length of the series: ");
        scanf("%d", &n);
        printf("The series is\n");
        for(i = 1; i <= n; i++)
        {
            j += 0.9/pow(10, i-1);
            printf("%f, ", j);
            sum += j;
        }
        printf("\nSum of  the series is %f", sum);
        system ("pause");
        return 0;
    }

    Sample Run:

    Enter the length of the series: 5
    The series is
    0.900000, 0.990000, 0.999000, 0.999900, 0.999990,
    Sum of  the series is 4.888890

    Program to decide the type of a triangle.

    Problem:

    Given the lengths of three sides of a triangle. Write a program in C which decides the type (Right Angled, Acute, Obtuse, Isosceles, Scalene, Equilateral etc.) of the triangle.

    Source Code:

    /*to decide the type of triangle*/
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        float a,b,c;
        printf("Enter the length of three sides of a triangle: ");
        scanf("%f%f%f",&a,&b,&c);
        if(a > b + c || b > c + a || c > a+b)
        {
            printf("This is not a triangle.");
            system ("pause");
            exit(0);
        }
        else if( a == b && b == c && c == a)
            printf("This is an equilateral triangle.");
        else if(a==b || b==c || c==a)
            printf("This is an isosceles triangle.");
        else if(a*a == b*b + c*c || b*b == c*c + a*a || c*c == a*a + b*b)
            printf("This is a right angled triangle.");
        else if(a*a > b*b + c*c || b*b > c*c + a*a || c*c > a*a + b*b)
            printf("This is an obtuse angled triangle.");
        else if(a*a < b*b + c*c || b*b < c*c + a*a || c*c < a*a + b*b)
            printf("This is an acute angled triangle.");
        else
            printf("This is a scalene triangle.");
        system ("pause");
        return 0;
    }

    Sample Output:

    Enter the length of three sides of a triangle: 5 6 7
    This is an acute angled triangle.

    Program to display the number in reverse order.

    Problem:

    WAP to read four digit number and display the number in reverse order.


    Source Code:

    /*to display the numbers in reverse order*/
    #include<stdio.h>
    int main()
    {
    int n;
    printf("Enter a four digit number: ");
    scanf("%d", &n);
     printf("The number in reverse order is ");
    printf("%d", n %10);
    n = n/10;
    printf("%d", n %10);
    n = n/10;
    printf("%d", n %10);
    n = n/10;
    printf("%d", n %10);
    return 0;
    }

    Sample Run:

    Enter a four digit number: 1234
    The number in reverse order is 4321


    Program to find net salary

    Problem:

    WAP to read basic salary. Apart from basic salary, allowances are given as follows
    a. TA (Travelling Allowance) is 5% of basic salary
    b. DA (Daily Allowance) is 3% of basic salary
    c. HRA (House Rate Allowance) is 8% of basic salary
    Also, tax is witheld 1% of basic salary.
    Finally, calculate the net salary


    Source Code:

    /*to find net salary*/
    #include<stdio.h>
    int main()
    {
    float bs, ta, hra, da, tax, ns;
    printf("Enter the amount of basic salary: ");
    scanf("%f", &bs);
    ta = 0.05 * bs;
    da = 0.03 * bs;
    hra = 0.08 * bs;
    tax = 0.01 * bs;
    ns = bs + ta + da + hra - tax;
    printf("Travelling Allowance = %f", ta);
    printf("\nDaily Allowance = %f", da);
    printf("\nHome Rate Allowance = %f", hra);
    printf("\nTax = %f", tax);
    printf("\nNet Salary = %f", ns);
    return 0;
    }

    Sample Run:

    Enter the amount of basic salary: 50000
    Travelling Allowance = 2500.00
    Daily Allowance = 1500.00
    Home Rate Allowance = 4000.00
    Tax = 500.00
    Net Salary = 57500.00