/// <summary> Process this request </summary> /// <param name="Context"></param> public void ProcessRequest(HttpContext Context) { Context.Response.ContentType = "text/plain"; Context.Response.Expires = -1; // Try to get the security token key string tokenKey = Context.Request["token"]; if (tokenKey == null) { Context.Response.Write("No token provided with this request"); Context.Response.StatusCode = 401; return; } // Try to get the matching token object from the session UploadiFive_Security_Token tokenObj = Context.Session["#UPLOADIFIVE::" + tokenKey] as UploadiFive_Security_Token; if (tokenObj == null) { Context.Response.Write("No matching server-side token found for this request"); Context.Response.StatusCode = 401; return; } try { // Get the posted file from the appropriate file key HttpPostedFile postedFile = Context.Request.Files[tokenObj.FileObjName]; if (postedFile != null) { // Get the path from the token and ensure it exists string path = tokenObj.UploadPath; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } // Get the filename for the uploaded file string filename = Path.GetFileName(postedFile.FileName); // Should this be overriden? if (!String.IsNullOrEmpty(tokenObj.ServerSideFileName)) { filename = tokenObj.ServerSideFileName; } // Are there file extension restrictions? if (!String.IsNullOrEmpty(tokenObj.AllowedFileExtensions)) { string extension = Path.GetExtension(postedFile.FileName).ToLower(); List <string> allowed = tokenObj.AllowedFileExtensions.Split("|,".ToCharArray()).ToList(); if (!allowed.Contains(extension)) { Context.Response.Write("Invalid extension"); Context.Response.StatusCode = 401; return; } } // Ensure file does not already exist string newFileName = Path.Combine(path, filename); if (File.Exists(newFileName)) { File.Delete(newFileName); } // Save this file locally postedFile.SaveAs(newFileName); // Post a successful status Context.Response.Write(filename); Context.Response.StatusCode = 200; } } catch (Exception ex) { Context.Response.Write("Error: " + ex.Message); Context.Response.StatusCode = 500; } }
/// <summary> Writes the file input and script necessary for the upload of the files </summary> /// <param name="Output"> Stream to write to </param> protected override void RenderContents(HtmlTextWriter Output) { // If there is no current HTTPContext, can't do this... if ((UploadPath.Length > 0) && (HttpContext.Current != null)) { // Create a new security token, save in session, and set token GUID in the form data UploadiFive_Security_Token newToken = new UploadiFive_Security_Token(UploadPath, AllowedFileExtensions, FileObjName, ServerSideFileName, ReturnToken); FormData["token"] = newToken.ThisGuid.ToString(); HttpContext.Current.Session["#UPLOADIFIVE::" + newToken.ThisGuid.ToString()] = newToken; } // Add the file input element Output.Write("<input id=\"" + FileInputID + "\" name=\"" + FileInputID + "\" "); if (FileInputClass.Length > 0) Output.Write("class=\"" + FileInputClass + "\" "); Output.WriteLine("type=\"file\" />"); Output.WriteLine(); // Add the script for this to be added to the ready document event Output.WriteLine("<script type=\"text/javascript\">"); Output.WriteLine(" $(document).ready(function() {"); // Allow the settings object to write the actual jquery settings.Add_To_Stream(Output, String.Empty, String.Empty); Output.WriteLine(" });"); Output.WriteLine("</script>"); Output.WriteLine(); }