Functionality for uploading and displaying attachments or files.
MvcHtmlString Attachments(this HtmlHelper helper,
object htmlAttributes = null, string imagePage = "/ajax/ImagePreview",
List<Attachment> attachments = null,
string downloadFunction = "DownloadFile", string deleteFunction = "DeleteFile",
bool readOnly = false, string buttonText = "Add Attachment"),
bool hoverResize = true)
The location to the page which renders the attachment image.
The existing list of attachments to render.
Javascript function called to download a file.
Javascript function called to delete a file.
Make the control read only.
The text on the upload button.
Cause preview images to go full size when hovered over.
ShareItLibrary.Attachment.InitFileUpload (uploadUrl, renderFunction);
The C# function which will upload the files.
The javascript function to render files.
ShareItLibrary.Attachment.RenderAttachments (attachments, imagePage, downloadFunction, deleteFunction);
The json data which holds the list of attachments.
The C# function to render the image.
The javascript function to donload files.
The javascript function to delete files.
@Html.Attachments(attachments: (List<Attachment>) ViewBag.Attachments, downloadFunction: "MyApp.Controller.DownloadFile", imagePage: "/Controller/Action", deleteFunction: "MyApp.Controller.DeleteFile", readOnly: readOnly)
On Init
if ($('#FileUploadBox').length) { ShareItLibrary.Attachment.InitFileUpload('/Ajax/UploadFiles', MyApp.Controller.RenderFiles);}[HttpGet]public ActionResult Download(Guid aid){ var Attachment = db.AttachmentGet(aid, null, null, User.UserId()).SingleOrDefault(); if (Attachment != null) return File(Attachment.Attachment, SiUtil.ContentTypeFromFilename(Attachment.Filename), Attachment.Filename); else return null;}[HttpPost]public ActionResult DeleteFile(Guid aid){ var Attachment = db.AttachmentDelete(aid, User.UserId()); return Json("");}[HttpPost]public JsonResult Attachments(Guid? issueId, Guid? discussionId){ var attachments = db.AttachmentGet(null, issueId, discussionId, User.UserId()).ToList(); return Json(attachments);}
@Html.Attachments(attachments: (List<Attachment>) ViewBag.Attachments, downloadFunction: "MyApp.JsController.DownloadFile", imagePage: "[/Controller/]Action")
On Init
MyApp.JsController.Init = function () { ShareItLibrary.Attachment.AddFormData("FileId", $('#FileId').val()); ShareItLibrary.Attachment.InitFileUpload('UploadFiles', MyApp.JsController.RenderFiles); MyApp.JsController.RenderFiles();};MyApp.JsController.DownloadFile = function (fileId) { window.open('Download?aid=' + fileId);};MyApp.JsController.RenderFiles = function () { var headers = {}; headers['__RequestVerificationToken'] = $('[name="__RequestVerificationToken"]').val(); $.ajax({ type: "POST", url: '/Controller/Files', headers: headers, data: 'FileId=' + $('#FileId').val(), dataType: "json", error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); } }).done(function (data) { ShareItLibrary.Attachment.RenderAttachments(data, 'Download', 'MyApp.JsController.DownloadFile', 'MyApp.JsController.DeleteFile'); });};[HttpPost]public ActionResult EditSet(FileGet_Result file){ if (ModelState.IsValid) { db.FileSet(file.FileId, file.FileType, null, null); ... }}[HttpPost]public ActionResult Files(Guid? fileId){ var files = db.FileGet(fileId, null).Select(f => new { Filename = f.FileName, AttachmentId = f.FileId }).ToList(); return Json(files);}[HttpGet]public ActionResult Download(Guid aid){ var file = db.FileGet(aid, null).SingleOrDefault(); if (file != null) return File(file.FileData, SiUtil.ContentTypeFromFilename(file.FileName), file.Filename); else return null;}[HttpPost]public JsonResult UploadFiles(Guid? fileId){ var files = Request.Files; foreach (string key in files) { var file = files[key]; string filename = file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1); byte[] fileData = new byte[file.InputStream.Length]; file.InputStream.Read(fileData, 0, fileData.Length); db.FileSet(fileId, null, filename, fileData); } return Json("");}
@Html.Attachments(attachments: (List<Attachment>) ViewBag.Attachments, downloadFunction: "MyApp.Controller.DownloadFile", imagePage: "/Controller/Action", deleteFunction: "MyApp.Controller.DeleteFile", readOnly: readOnly)
On Init
if ($('#FileUploadBox').length) { ShareItLibrary.Attachment.AddFormData("MyId", $('#MyId').val()); ShareItLibrary.Attachment.AddFormData("MySecondId", $('#MySecondId').val()); ShareItLibrary.Attachment.InitFileUpload('/Ajax/UploadFiles', MyApp.Controller.RenderFiles);}[HttpGet]public ActionResult Download(Guid aid){ var Attachment = db.AttachmentGet(aid, null, null, User.UserId()).SingleOrDefault(); if (Attachment != null) return File(Attachment.Attachment, SiUtil.ContentTypeFromFilename(Attachment.Filename), Attachment.Filename); else return null;}[HttpPost]public ActionResult DeleteFile(Guid aid){ var Attachment = db.AttachmentDelete(aid, User.UserId()); return Json("");}[HttpPost]public JsonResult Attachments(Guid? myId, Guid? mySecondId){ var attachments = db.AttachmentGet(null, myId, mySecondId, User.UserId()).ToList(); return Json(attachments);}
@Html.Attachments(null, buttonText: "Upload File")
On Init
if ($('#FileUploadBox').length) { ShareItLibrary.Attachment.InitFileUpload('/Controller/UploadFiles', function (d) { if (d.Total > 0) { $('#BulkUpload').val('True'); $('#BulkUpload').closest('form').submit(); } else window.location.reload(); }[HttpGet]public JsonResult UploadFiles(){ var files = Request.Files; foreach (string key in files) ImportFile(files[key]); return Json("");}private void ImportFile(HttpPostedFileBase file){ StreamReader csvreader = new StreamReader(file.InputStream); CsvHelper.CsvReader csv = new CsvHelper.CsvReader(csvreader); var records = csv.GetRecords<MyCsv>().ToList(); ... do stuff with records.}