I have used this jQuery pluging several times.
I put my download button in the jQuery UI modal dialog , which uses the PopupImageUploader element.
<div id="PopupImageUploader" title="Upload Image"> <div id="uploaderFile"></div> </div>
and my javascript builds the loader on the upladerFile element
function CreateImageUploader() { var uploader = new qq.FileUploader({ element: $('#uploaderFile')[0], template: '<div class="qq-uploader">' + '<div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>' + '<div class="qq-upload-button ui-button ui-widget ui-corner-all ui-button-text-only ui-state-default">Seleziona il Listino Excel</div>' + '<ul class="qq-upload-list"></ul>' + '</div>', hoverClass: 'ui-state-hover', focusClass: 'ui-state-focus', action: 'Home/UploadImage', allowedExtensions: ['jpg', 'gif'], params: { }, onSubmit: function(file, ext) { }, onComplete: function(id, fileName, responseJSON) { $("#PopupImageUploader").dialog('close'); } } }); }
You can use the onComplete event, check the download result and, ultimately, update the drop-down menu. The UploadImage action can take additional parameters specified in the params: { } property params: { } . This is my controller:
[HttpPost()] public System.String UploadImage(string id) { bool IsIE = false; string sFileName = ""; var TempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_TEMP"); if ((Request.Files == null) || (Request.Files.Count == 0)) { if (string.IsNullOrEmpty(Request.Params["qqfile"])) { return ("{success:false, error:'request file is empty'}"); } else { sFileName = Request.Params["qqfile"].ToString(); } } else { sFileName = Request.Files[0].FileName; IsIE = true; } if (string.IsNullOrEmpty(sFileName)) { return ("{success:false, error:'request file is empty'}"); } string DocumentName = id + Path.GetExtension(sFileName); if (IsIE) { try { Request.Files[0].SaveAs(Path.Combine(TempFolder, DocumentName)); } catch (Exception ex) { return ("{success:false, error:'" + ex.Message + "'}"); } } else { try { if ((Request.InputStream != null) && (Request.InputStream.CanRead) && (Request.InputStream.Length > 0)) { using (FileStream fileStream = new FileStream(Path.Combine(TempFolder, DocumentName), FileMode.Create)) { byte[] FileBytes = new byte[Convert.ToInt32(Request.InputStream.Length) + 1]; Int32 bytesRead = 0; bytesRead = Request.InputStream.Read(FileBytes, 0, FileBytes.Length); fileStream.Write(FileBytes, 0, bytesRead); fileStream.Flush(); fileStream.Close(); } } } catch (Exception ex) { return ("{success:false, error:'" + ex.Message + "'}"); } } var newFileName = "new assigned filename"; return ("{success:true, newfilename: '" + newFileName + "'}"); }
IE has a different behavior, so I have two different procedures for reading a file.