JQuery image upload & refresh using an ASHX file.
I recently finished a solution that uploads an image to my server, and then displays the uploaded image to the user. this is all done “asynchrously”, and it’s within an update panel to boot.
There are a good handful of solutions for doing this. One of the most popular is SWFUpload but I didn’t want to use any flash in my solution – some customers will not have it or it is blocked by some agencies… yes in the real world large agencies and corporations still use IE6 so HTML5… I’ll see you in a few years… back on track.
For the image upload I used a simple JQuery script written by Andris Valums. Out of all the scripts I looked at, this was the most straight forward, easy to implement, and had no inner tweaks that could cause issues in the future.
- function initSignature(userID) {
- refreshSignature(userID);
- var button = $('#dUpload'), interval;
- new AjaxUpload("#dUpload", {
- action: "../../handlers/ProcessSignature.ashx",
- name: "Signature",
- data: { "UserID": userID, "Type": "Upload" },
- obSubmit: function (file, ext) {
- if (ext && /^(bmp|jpg|png|jpeg|gif)$/.test(ext)) {
- // change button text, when user selects file.
- button.text("Uploading");
- this.disable();//don't upload any more files.
- interval = window.setInterval(function () {
- var text = button.text();
- if (button.text().length < 13)
- button.text(button.text() + ".");
- else
- button.text("Uploading");
- $("#dMessage").text("Uploading " + file);
- }, 200);
- }
- else {
- //Extension is not allowed.
- $("#dMessage").text("Error: only images are allowed");
- return false;
- }
- },
- onComplete: function (file, response) {
- button.text("Upload");
- //although plugins emulates hover effect automatically it doesn't work when button is disabled
- button.removeClass("hover");
- window.clearInterval(interval);
- $("#dMessage").text(response);
- if (response.indexOf("successful") > -1) {
- refreshSignature(userID);
- }
- //enable upload button
- this.enable();
- }
- });
- }
Seeing this is inside an update panel I wrapped the initialization script inside a function which I call from the script manager when the UpdatePanel is updated.
Once the image is uploaded I refresh the image by clearing out the containing div and recreating the image element. This is a tricky one. Even though the image element was deleted and recreated, the SRC was the same so browsers were caching the image. I decided to throw in an extra random value to the URL so the browser would be forced to get the newest image.
- function refreshSignature(userID) {
- $("#signature").hide();
- var sPath = "../../handlers/ProcessSignature.ashx?type=View&UserID=" + userID + "&d=" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
- $("#signature").attr({ src: sPath });
- setTimeout(function () { $("#signature").imageScale({ maxWidth:450, maxHeight: 50 }) }, 1000);
- $("#signature").fadeOut(100).delay(2000).fadeIn(1000);
- }
At the end I scale the image using a very simple JQuery image scaling plug-in. Below are the JS script files I used.
- Image Scaling JS - http://bicnet.org/files/jquery.custom.js
- File Upload JS - http://bicnet.org/files/fileuploader.js
Hope this helps anyone looking to do something similar.