Uploadify is a jQuery plugin that integrates a fully-customizable multiple file upload utility on your website. It uses a mixture of Javascript, ActionScript, and any server-side language to dynamically create an instance over any DOM element on a page.
The jQuery Uploadify website gives us ready example for PHP, but leaves us clueless on how to implement the same in .NET or even in SharePoint
So, here I am presenting you with example of C# for SharePoint implementation (upload.ashx) for the jQuery Uploadify plugin.
How to add it to SharePoint
- Download the latest stable version of Uploadify
- Extract everything to a desktop folder
- Create a new SharePoint Empty Project
- Add a new Visual Web Part
- Add the SharePoint “Layouts” Mapped folder
- Add all uploadify extracted files to this folder, except the php files
- Add the following source code to you user control
<script src="/_layouts/Uploadify/jquery-1.4.2.min.js" type="text/javascript"></script> <script src="/_layouts/Uploadify/jquery.uploadify.v2.1.4.js" type="text/javascript"></script> <script src="/_layouts/Uploadify/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script> <script src="/_layouts/Uploadify/swfobject.js" type="text/javascript"></script> <link href="/_layouts/Uploadify/uploadify.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> // <'[CDATA[ $(document).ready(function () { $("#fileInput").uploadify({ 'uploader': '/_layouts/Uploadify/uploadify.swf', 'cancelImg': '/_layouts/Uploadify/cancel.png', 'buttonText': 'Browse Files', 'script': '/_layouts/Uploadify/Upload.ashx', 'scriptData': { }, 'fileDesc': 'Document Files', 'fileExt': '*.doc;*.docx;*.pdf', 'multi': true, 'auto': true, 'queueSizeLimit': 5, 'sizeLimit': 4000000, 'folder': '/uploads', 'onAllComplete': function (event, queueID, fileObj, response, data) { alert(response); } }); }); // ]] </script> <input id="fileInput" name="fileInput" type="file" /> - Create a new ashx file in SharePoint “Layouts” Mapped Folder named Upload.ashx
<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ WebHandler Language="C#" Class="Upload" %> using System; using System.Web; using Microsoft.SharePoint; using System.IO; using System.Collections; public class Upload : IHttpHandler { /// <summary> /// You will need to configure this handler in the web.config file of your /// web and register it with IIS before being able to use it. For more information /// see the following link: http://go.microsoft.com/?linkid=8101007 /// </summary> #region IHttpHandler Members public bool IsReusable { // Return false in case your Managed Handler cannot be reused for another request. // Usually this would be false in case you have some state information preserved per request. get { return false; } } public void ProcessRequest(HttpContext context) { try { string result = "success"; HttpPostedFile hfc = context.Request.Files["Filedata"]; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite objSPSite = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb objSPWeb = objSPSite.OpenWeb()) { SPFolder myLibrary = objSPWeb.Folders["Documents"]; if (myLibrary != null) { objSPWeb.AllowUnsafeUpdates = true; //Add a new item // Prepare to upload Boolean replaceExistingFiles = false; string file_name = Path.GetFileName(hfc.FileName); byte[] file_content = new byte[Convert.ToInt32(hfc.ContentLength)]; hfc.InputStream.Read(file_content, 0, Convert.ToInt32(hfc.InputStream.Length)); // Upload document SPFile spfile = myLibrary.Files.Add(file_name, file_content, replaceExistingFiles); // Commit myLibrary.Update(); objSPWeb.AllowUnsafeUpdates = false; } } } }); context.Response.ContentType = "text/plain"; context.Response.Write(result); } catch (Exception ex) { context.Response.ContentType = "text/plain"; context.Response.Write("Error: " + ex.Message); } } #endregion }
And that’s it! :)
I did this with the help of this Video Tutorial by Cason Clagg.








Leave a comment