Click here to Skip to main content
Licence CPOL
First Posted 22 Jun 2011
Views 12,376
Downloads 1,152
Bookmarked 43 times

Lightweight Image Upload with Animation

By | 22 Jun 2011 | Article
This tutorial is going to guide you in creating a rich application for Upload, View and Delete images with special effects.

User Interface

Animated_Screen.gif

Introduction

This tutorial is going to guide you in creating a rich application for Upload, View and Delete images with special effects. You must have followed many tutorials to help you upload files using .NET or (AJAX or JQuery). Now I combined both of these elements to create one complete application.

Used Library from JQuery

jquery-1.5.1.js (JQuery framework for special effects) ajaxupload.3.5.js (for upload file with silent mode) you can download latest files from http://jquery.com/.

What's Surprising in this Article?

I am showing you a surprising thing... I have not used any File Upload control and this magic was done by using JQuery.

Using the Code

Default.aspx (which contains User Interface) the following function is dynamically creating interface for upload Image without File Upload control.

$().ready(function () {
            var counter = 0;
            $(function () {
                var btnUpload = $('#addImage');
                new AjaxUpload(btnUpload, {
                    action: 'saveupload.aspx',
                    name: 'uploadfile',
                    onSubmit: function (file, ext) {
                        $("#loading").show();
                    },
                    onComplete: function (file, response) {
                        var uploadedfile = "UserData/" + file;
                        $("#uploadImageWrapper").
                        append("<div class='imageContainer offset'  
                        id='current" + counter + "'>
                        <img height='65px' width='65px' src='" + 
                        uploadedfile + "' alt='" + uploadedfile + 
                        "'/><div id='close" + counter + "' 
                        class='close'  title='" + uploadedfile + "'  
                        önclick='RemoveImage(this);'><a ></a></div></div>");
                        $('#current' + counter).fadeIn('slow', function () {
                            $("#loading").hide();
                            $("#message").show();
                            $("#message").html("Added successfully!");
                            $("#message").fadeOut(3000);
                            counter++;
                        });
                    }
                });
            });
        });

In the above method, I have defined action: 'saveupload.aspx'. That means whenever you select any file from File Dialog, it will automatically call saveupload.aspx files Page_load method, and this function gets all the requested files (in my case, it is only one at a time) and saves at a specific location (in my case \UserData\). The following code is very self explanatory (for saving file on server saveupload.aspx.cs).

 protected void Page_Load(object sender, EventArgs e)
    {
        HttpFileCollection uploadedFiles = Request.Files;
        int i = 0;
        if (uploadedFiles.Count > 0)
        {
            while (!(i == uploadedFiles.Count))
            {
                HttpPostedFile userPostedFile = uploadedFiles[i];
                if (userPostedFile.ContentLength > 0)
                {
                    string filename = userPostedFile.FileName.Substring
			(userPostedFile.FileName.LastIndexOf("\\") + 1);
                    userPostedFile.SaveAs(Path.Combine
			(Server.MapPath("UserData"), filename));
                }
                i += 1;
            }
        }
    }

After successfully saving file on server, you should be able to see message on bottom of the Image Uploader "Added successfully!". This message will automatically be closed after 3 seconds with transition effect. The following code is specifically for delete file from server. I have defined url: "removeupload.aspx?filename". That means whenever you clicked on X mark (you can see it over every uploaded image), it will pass filename to the server for deleting the file.

function RemoveImage(_this) {
            var counter = _this.id.replace('close', '');
            $("#loading").show();
            $.ajax({
                type: "POST",
                url: "removeupload.aspx",
                data: "filename=" + _this.getAttribute('title'),
                success: function (msg) {
                    $('#current' + counter).fadeOut('slow', function () {
                        $("#loading").hide();
                        $("#message").show();
                        $("#message").html("Removed successfully!");
                        $("#message").fadeOut(3000);
                    });
                }
            });
        }

The following code is very self explanatory (for deleting file on server removeupload.aspx.cs):

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string filename = Server.MapPath(Request.Params["filename"]);
            FileInfo TheFile = new FileInfo(filename);
            if (TheFile.Exists) File.Delete(filename);
        }
        catch (Exception ex)
        {
        }
    }

Thing For You To Do

  • Try to check for file type validation
  • Exception handling

Your Thoughts

If you find some issues or bugs with it, just leave a comment or drop me an email. If you make any notes on this, let me know that too so I don't have to redo any of your hard work. Please provide a "Vote" if this would be helpful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sunasara Imdadhusen

Software Developer (Senior)
Verve Systems Ahmedabad-Gujarat
India India

Member

Aspiring for a challenging carrier wherein I can learn, grow, expand and share my existing knowledge in meaningful and coherent way.

sunaSaRa Imdadhusen
+91 99095 44184
 
AWARDS:
  1. 1st Best Asp.Net article of SEP 2010
  2. 2nd Best Asp.Net article of MAY 2011
 
Read More Articles...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAbout the control PinmemberAli Al Omairi(Abu AlHassan)4:09 15 Sep '11  
AnswerRe: About the control PinmemberSunasara Imdadhusen1:45 14 Dec '11  
GeneralRe: About the control PinmemberAli Al Omairi(Abu AlHassan)2:25 28 Jan '12  
QuestionExcellent PinmemberSavalia_Manoj4:08 2 Sep '11  
AnswerRe: Excellent PinmemberSunasara Imdadhusen3:22 13 Sep '11  
QuestionUsing extra data and response type PinmemberAli Al Omairi(Abu AlHassan)3:54 11 Jul '11  
AnswerRe: Using extra data and response type PinmemberSunasara Imdadhusen4:01 13 Jul '11  
GeneralRe: Using extra data and response type PinmemberAli Al Omairi(Abu AlHassan)0:28 7 Aug '11  
GeneralRe: Using extra data and response type PinmemberSunasara Imdadhusen16:54 7 Aug '11  
QuestionNew Update PinmemberAli Al Omairi(Abu AlHassan)6:36 8 Jul '11  
AnswerRe: New Update PinmemberSunasara Imdadhusen4:00 13 Jul '11  
GeneralRe: New Update PinmemberAli Al Omairi(Abu AlHassan)22:53 6 Feb '12  
GeneralMy vote of 5 Pinmemberramakrishnankt1:15 1 Jul '11  
GeneralRe: My vote of 5 PinmemberSunasara Imdadhusen18:23 1 Jul '11  
GeneralMy vote of 5 PinmemberOmar Gamil6:17 23 Jun '11  
GeneralRe: My vote of 5 PinmemberSunasara Imdadhusen18:36 23 Jun '11  
GeneralMy vote of 5 Pinmemberkelvin z20:13 22 Jun '11  
AnswerRe: My vote of 5 PinmemberSunasara Imdadhusen22:01 22 Jun '11  
QuestionGood Article Pinmemberrahultandon100019:02 22 Jun '11  
AnswerRe: Good Article [modified] PinmemberSunasara Imdadhusen19:17 22 Jun '11  
QuestionMy vote is 5 PinmemberNajmul Hoda4:45 22 Jun '11  
AnswerRe: My vote is 5 [modified] PinmemberSunasara Imdadhusen18:30 22 Jun '11  
SuggestionGood Idea PinmemberAli Al Omairi(Abu AlHassan)3:43 22 Jun '11  
AnswerRe: Good Idea [modified] PinmemberSunasara Imdadhusen18:28 22 Jun '11  
GeneralRe: Good Idea PinmemberAli Al Omairi(Abu AlHassan)10:14 23 Jun '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120528.1 | Last Updated 22 Jun 2011
Article Copyright 2011 by Sunasara Imdadhusen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid