Playing with jQuery, Uploadify and LightBox

Recently I started working on a project in ASP.NET and one of the “features” I had to implement was to allow the user to upload photos. As user experience is important, I didn’t want to have a simple page which would post back refreshing the page, or just a “one by one” upload process.

Since the FileUpload control is one of the controls that don’t work inside the Update Panel, I had to look at some other options. As I am still learning jQuery and trying to improve in this area, I decided to look for some upload plugin written in jQuery. After some research on the Internet I managed to find Uploadify which seems like one of the best options out there: simple, good looking and easy to play with.

In one of my previous blog entries I was showing how to upload, store an image to a database and then retrieve it and add a watermark to the image. I will use this article as a starting point for this new application and just modify it in order to make the upload process more user friendly.

First step to use the Uploadify plugin is to import it to our project and in order to use it we need to use a simple input element of type “file”. Once we defined the input element in our page we need to call the uploadify method on it and specify the default values we want. After this small step, the upload page should look like:

HTML
  1. <form id=“form1” runat=“server”>
  2. <div>
  3. <script type=“text/javascript”>
  4. // <![CDATA[
  5. var id = “55”;
  6. var theString = “asdf”;
  7. $(document).ready(function() {
  8. $(‘#fileInput’).uploadify({
  9. ‘uploader’: ‘uploadify/uploadify.swf’,
  10. ‘script’: ‘Upload.ashx’,
  11. ‘scriptData’: { ‘id’: id, ‘foo’: theString },
  12. ‘cancelImg’: ‘uploadify/cancel.png’,
  13. ‘auto’: false,
  14. ‘multi’: true,
  15. ‘fileDesc’: ‘Image Files’,
  16. ‘fileExt’: ‘*.jpg;*.png;*.gif;*.bmp;*.jpeg’,
  17. ‘queueSizeLimit’: 5,
  18. ‘sizeLimit’: 512000,
  19. ‘buttonText’: ‘Choose Images’,
  20. ‘folder’: ‘/uploads’,
  21. ‘onComplete’: function(event, queueID, fileObj, response, data) {
  22. },
  23. ‘onAllComplete’: function(event, queueID, fileObj, response, data) {
  24. }
  25. });
  26. });
  27. // ]]></script>
  28. <input id=“fileInput” name=“fileInput” type=“file” />
  29. <a href=“javascript:$(‘#fileInput’).uploadifyUpload();”>Upload Files</a>
  30. </div>
  31. </form>

I will not go into explaining all the parameters used for setting up Uploadify, I will just highlight 3 parameters that were of interest for me:

  • queueSizeLimit: according to the official documentation it allows you to set the maximum number of files that can exist in the queue at one time.  If the user tries to add more the the maximum limit, the onQueueFull function is triggered.  The multioption should be used in conjunction with this option.
  • sizeLimit: allows you to set the maximum file size allowed per file in bytes.  If the file exceeds the size limit, it will return an error on upload.
  • script: is required and points to the back-end script that will process your files.  The back-end script can be written in any server-side language.

As you can see I set my upload control to allow a maximum of 5 files at one time and the maximum size allowed for one file is 512000 bytes. The Upload.ashx script holds the server code which is responsible for getting the image the user tries to upload, store it in the database and reply back to the client. Since we allow the user to upload an image, I want to allow them to delete the posted image in case they changed their mind. And I want to do it by the simple click of the mouse, no page postbacks… and why not show the uploaded images in an elegant way with LightBox?

Well, let’s try to do that…

We’ll need to modify our upload page just a bit to accommodate an area where the images will be displayed after upload and also add a small piece of code which will display the images in case of successful upload – the code which will display the images will be send as response by the Upload.ashx script. I added one div element at the end of my previous code and gave it an id=”uploaded_photos” and also modified the code in the onComplete event of the uploadify method (changes are in italic font):

HTML
  1. <form id=“form1” runat=“server”>
  2. <div>
  3. <script type=“text/javascript”>
  4. // <![CDATA[
  5. var id = “55”;
  6. var theString = “asdf”;
  7. $(document).ready(function() {
  8. $(‘#fileInput’).uploadify({
  9. ‘uploader’: ‘uploadify/uploadify.swf’,
  10. ‘script’: ‘Upload.ashx’,
  11. ‘scriptData’: { ‘id’: id, ‘foo’: theString },
  12. ‘cancelImg’: ‘uploadify/cancel.png’,
  13. ‘auto’: false,
  14. ‘multi’: true,
  15. ‘fileDesc’: ‘Image Files’,
  16. ‘fileExt’: ‘*.jpg;*.png;*.gif;*.bmp;*.jpeg’,
  17. ‘queueSizeLimit’: 5,
  18. ‘sizeLimit’: 512000,
  19. ‘buttonText’: ‘Choose Images’,
  20. ‘folder’: ‘/uploads’,
  21. ‘onComplete’: function(event, queueID, fileObj, response, data) {
  22. $(‘#uploaded_photos’).append(response);
  23. },
  24. ‘onAllComplete’: function(event, queueID, fileObj, response, data) {
  25. }
  26. });
  27. });
  28. // ]]></script>
  29. <input id=“fileInput” name=“fileInput” type=“file” />
  30. <a href=“javascript:$(‘#fileInput’).uploadifyUpload();”>Upload Files</a>
  31. </div>
  32. </form>
  33. <div id=“uploaded_photos”></div>

Since our upload page is ready we need to start writing some code to get the images, store them and reply to the client in case the upload is successful. To add a new generic handler right-click on the project and then select Add –> New Item. In the dialog window that appears select Generic Handler and name it Upload.ashx. The code for the Upload.ashx looks like this:

C#
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Text;
  7. namespace UploadImages
  8. {
  9. /// <summary>
  10. /// Summary description for $codebehindclassname$
  11. /// </summary>
  12. [WebService(Namespace = “http://tempuri.org/”)]
  13. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  14. public class Upload : IHttpHandler
  15. {
  16. public void ProcessRequest(HttpContext context)
  17. {
  18. try
  19. {
  20. HttpPostedFile file= context.Request.Files[“Filedata”];
  21. byte[] image_data = new byte[file.ContentLength];
  22. file.InputStream.Read(image_data, 0, file.ContentLength);
  23. ImagesDbEntities2 ent = new ImagesDbEntities2();
  24. images img = new images();
  25. img.name = file.FileName;
  26. img.data = image_data;
  27. img.type = file.ContentType;
  28. ent.AddToimages(img);
  29. ent.SaveChanges();
  30. #region update the number of images the user can upload
  31. int imgs = 5; // the number of images by default – in case of error set to 0
  32. // try to limit the allowed images to 5
  33. IEnumerable<images> images = from i in ent.images select i;
  34. if (images != null && images.Count() != 0)
  35. {
  36. imgs = 5 images.Count();
  37. if (imgs < 0) imgs = 0;
  38. }
  39. else
  40. imgs = 5;
  41. #endregion
  42. // create the delete link ID which will be used in the jquery selector
  43. string deleteLinkID = “deleteImage” + img.id;
  44. StringBuilder sb = new StringBuilder();
  45. sb.Append(“<script>”);
  46. sb.Append(“$(\”a[class=gallery]\”).lightBox();”);
  47. sb.Append(“$(‘#fileInput’).uploadifySettings(‘queueSizeLimit’,” + imgs.ToString() + “);”);
  48. sb.Append(“$(\”#” + deleteLinkID + \”).click(function(){ “);
  49. sb.Append(“if(confirm(\”Are you sure you want to delete the image?\”)) { “);
  50. // start creating the script which will delete the image by using AJAX call to a web service
  51. sb.Append(“$.ajax({“);
  52. sb.Append(“type: \”POST\”,”);
  53. // define the service method that will be used to delete the image
  54. sb.Append(“url: \”ImagesService.asmx/DeleteImage\”,”);
  55. sb.Append(“data: \”{id: “ + img.id + “}\”,”);
  56. sb.Append(“contentType: \”application/json; charset=utf-8\”,”);
  57. sb.Append(“dataType: \”json\”,”);
  58. sb.Append(“success: function(msg) {“);
  59. // if the call is successful append the result so we’ll hide the image and update the# of images the user can select
  60. sb.Append(“if(msg.d != \”\”) { $(\”#uploaded_photos\”).append(msg.d); }”);
  61. sb.Append(“}});”);
  62. sb.Append(” } “); // the matching } for the if(confirm)
  63. sb.Append(” })”);
  64. sb.Append(“</script>”);
  65. string response = “<div id=\”divImage” + img.id +
  66. \” style=\”float:left; padding: 5px; text-aling: center;\”><a class=\”gallery\”
  67. href=\”GetImage.aspx?id=” + img.id + \”><img src=\”GetImage.aspx?size=t&id=” +
  68. img.id + \”></a><br /><a id=\” + deleteLinkID +
  69. \” href=\”#\” title=\”Delete\”>Delete</a></div>” + sb.ToString();
  70. context.Response.Write(response);
  71. }
  72. catch(Exception ex)
  73. {
  74. context.Response.Write(“”);
  75. }
  76. }
  77. public bool IsReusable
  78. {
  79. get
  80. {
  81. return false;
  82. }
  83. }
  84. }
  85. }

The last step is to create the ImagesService, by right-clicking on the project and then selecting Add –> New Item. In the dialog window that appears select Web Service and name it ImagesService with the .asmx extension. I had to uncomment one line in the file which was generated: I had to remove the comment for the [System.Web.Script.Services.ScriptService] attribute.

After removing the default method created generated by Visual Studio for me, I created my DeleteImage method which is called by the jQuery AJAX method in the Upload.ashx handler:

C#
  1. [WebMethod]
  2. public string DeleteImage(string id)
  3. {
  4. try
  5. {
  6. long imageID = long.Parse(id);
  7. ImagesDbEntities2 ent = new ImagesDbEntities2();
  8. images image = (from img in ent.images where img.id == imageID select img).FirstOrDefault();
  9. if (image != null)
  10. {
  11. ent.DeleteObject(image);
  12. ent.SaveChanges();
  13. }
  14. #region update the number of images the user can upload
  15. int imgs = 5; // the number of images by default – in case of error set to 0
  16. // try to limit the allowed images to 5
  17. IEnumerable<images> images = from i in ent.images select i;
  18. if (images != null && images.Count() != 0)
  19. {
  20. imgs = 5 images.Count();
  21. if (imgs < 0) imgs = 0;
  22. }
  23. else
  24. imgs = 5;
  25. #endregion
  26. StringBuilder sb = new StringBuilder();
  27. sb.Append(“<script>”);
  28. // update the number of images the user can select
  29. sb.Append(“$(‘#fileInput’).uploadifySettings(‘queueSizeLimit’,” + imgs.ToString() + “);”);
  30. sb.Append(“$(\”#divImage” + id + \”).fadeOut();”); // fadeout the image
  31. sb.Append(“</script>”);
  32. return sb.ToString();
  33. }
  34. catch (Exception ex)
  35. {
  36. return “”;
  37. }
  38. return “”; // by default
  39. }

In the screenshots below you can see the above code in action during the various steps:

default

1. the plugin before any action

images_selected

2. after some files were selected

images_uploaded

3. images inserted in the page after successful upload

lightbox_image

4. see images using the LightBox plugin

confirm_delete

5. clicking on the delete link first brings a confirmation dialog

after_delete

6. after deleting an image

The above pieces of code are working and can be used by someone looking to implement such a functionality in a project, but they can be extended and why not improved. In my real application I am associating the uploaded images to the logged in user, I send some additional info during the upload of images using the scriptData option of Uploadify plugin and of course I improved the looks of the page using some extra CSS.

Hope that the above pieces of code will be helpful for some and I am opened for suggestions or comments related to the code posted here.

UPDATE: I uploaded the code for this sample here.

Enjoy!