Attachments

Framework Core
@Html.Attachments()

Functionality for uploading and displaying attachments or files.

Constructors

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)

Properties

string imagePage
Default: "/ajax/ImagePreview"

The location to the page which renders the attachment image.

List<Attachment> attachments
Default: null

The existing list of attachments to render.

string downloadFunction
Default: "DownloadFile"

Javascript function called to download a file.

string deleteFunction
Default: "DeleteFile"

Javascript function called to delete a file.

bool readOnly
Default: false

Make the control read only.

string buttonText
Default: Add Attachment

The text on the upload button.

bool hoverResize
Default: true

Cause preview images to go full size when hovered over.

Javascript

ShareItLibrary.Attachment.InitFileUpload (uploadUrl, renderFunction);

Properties

uploadUrl

The C# function which will upload the files.

renderFunction

The javascript function to render files.

ShareItLibrary.Attachment.RenderAttachments (attachments, imagePage, downloadFunction, deleteFunction);

Properties

attachments

The json data which holds the list of attachments.

imagePage

The C# function to render the image.

downloadFunction

The javascript function to donload files.

deleteFunction

The javascript function to delete files.

Allow uploading, deleting of files.

Razor
@Html.Attachments(attachments: (List<Attachment>) ViewBag.Attachments, downloadFunction: "MyApp.Controller.DownloadFile", imagePage: "/Controller/Action", deleteFunction: "MyApp.Controller.DeleteFile", readOnly: readOnly)
Javascript

On Init

if ($('#FileUploadBox').length) {
ShareItLibrary.Attachment.InitFileUpload('/Ajax/UploadFiles', MyApp.Controller.RenderFiles);
}
C#
[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);
}

Allow uploading, deleting of single file.

Razor
@Html.Attachments(attachments: (List<Attachment>) ViewBag.Attachments, downloadFunction: "MyApp.JsController.DownloadFile", imagePage: "[/Controller/]Action")
Javascript

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');
});
};
C#
[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("");
}

Add variables to the file upload

Razor
@Html.Attachments(attachments: (List<Attachment>) ViewBag.Attachments, downloadFunction: "MyApp.Controller.DownloadFile", imagePage: "/Controller/Action", deleteFunction: "MyApp.Controller.DeleteFile", readOnly: readOnly)
Javascript

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);
}
C#
[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);
}

Upload Only

Razor
@Html.Attachments(null, buttonText: "Upload File")
Javascript

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();
}
C#
[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.
}