BoldSignA modern eSignature application with affordable pricing. Sign up today for unlimited document usage!
<script type="text/x-jsrender" id="columnTemplate"> <img style="width: 75px; height: 70px" src="/Content/ejThemes/images/Employees/{{:EmployeeID}}.png" alt="{{: EmployeeID }}" /> </script> <script type="text/template" id="template"> // Edit Template Form </script> @(Html.EJ().Grid<EmployeePhoto>("EmployeeGrid") .Datasource(ds => ds.URL("GetEmployeeData").InsertURL("PerformInsert").UpdateURL("PerformUpdate").RemoveURL("PerformDelete").Adaptor(AdaptorType.UrlAdaptor)) .EditSettings(e => e.AllowEditing().AllowDeleting().AllowAdding().EditMode(EditMode.DialogTemplate).DialogEditorTemplateID("#template")) .ToolbarSettings(tool => tool.ShowToolbar().ToolbarItems(item => { item.AddTool(ToolBarItems.Add); item.AddTool(ToolBarItems.Edit); item.AddTool(ToolBarItems.Delete); item.AddTool(ToolBarItems.Update); item.AddTool(ToolBarItems.Cancel); })) .Columns(col => { col.Field("EmployeeID").HeaderText("Employee ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add(); col.Field("Name").HeaderText("Name").TextAlign(TextAlign.Left).Width(75).Add(); //This column is rendered as template col.Field("Photo").HeaderText("Employee Image").Template(true).TemplateID("#columnTemplate").TextAlign(TextAlign.Center).Width(110).Add(); }) .ClientSideEvents(evt => evt.ActionBegin("onActionBegin").ActionComplete("onActionComplete"))) <script> $(function () { //Register helper to show file details $.views.helpers({ HasFile: HasFile }); }); function onActionBegin(args) { if (args.requestType == "save") { //Updating the file as one of the column data args.data["Photo"] = window.uploadData.files; window.uploadData["editData"] = new FormData(); //Converting the grid data to form data to send files to controller for (var d in args.data) uploadData.editData.append(d, args.data[d]); proxy = this; $.ajax({ url: window.uploadData.url, type: 'POST', data: window.uploadData.editData, contentType: false, processData: false, success: function () { proxy.refreshContent(); } }); } } function onActionComplete(args) { if (args.requestType == "beginedit" || args.requestType == "add") { //A Global var to track and hold file uploading info window.uploadData = {}; window.uploadData.url = args.requestType == "add" ? "PerformInsert" : "PerformUpdate"; //Render upload control $("#EmployeeGridPhoto").ejUploadbox({ saveUrl: "/Home/SaveDefault", removeUrl: "/Home/RemoveDefault", dialogAction: { closeOnComplete: true }, fileSelect: function (args) { //Get the file from fileSelect event uploadData["files"] = args[0].rawFile; } }); } } //Helper function to check file available status function HasFile() { return this.data["Photo"] != null ? "File Available" : "No File Available"; } public void SaveDefault(IEnumerable<HttpPostedFileBase> EmployeeGridPhoto) { foreach (var file in EmployeeGridPhoto) { var fileName = Path.GetFileName(file.FileName); var destinationPath = Path.Combine(Server.MapPath("~/Content/ejThemes/images/Employees"), fileName); file.SaveAs(destinationPath); } } public void RemoveDefault(string[] fileNames) { foreach (var fullName in fileNames) { var fileName = Path.GetFileName(fullName); var physicalPath = Path.Combine(Server.MapPath("~/Content/ejThemes/images/Employees"), fileName); if (System.IO.File.Exists(physicalPath)) { System.IO.File.Delete(physicalPath); } } |
function onActionComplete(args) {
if (args.requestType == "beginedit" || args.requestType == "add") {
//A Global var to track and hold file uploading info
window.uploadData = {};
window.uploadData.url = args.requestType == "add" ? "PerformInsert" : "PerformUpdate";
//Render upload control
$("#EmployeeGridPhoto").ejUploadbox({
saveUrl: "/Home/SaveDefault",
removeUrl: "/Home/RemoveDefault",
dialogAction: { closeOnComplete: true },
fileSelect: function (args) {
//Get the file from fileSelect event
uploadData["files"] = args["files"][0].rawFile; // for latest version
// if you are using older version 13.1.026 we suggest you to use args[0].rawFile;
var filename = args["files"][0].rawFile.name; // for latest version
// if you are using older version 13.1.026 we suggest you to use args[0].rawFile.name;
}
});
}
}
HomeController.cs
//Perform update
public void PerformUpdate(EmployeeInsertModel value)
{
..
}
|
$("#EmployeeGridPhoto").ejUploadbox({
saveUrl: "/Home/SaveDefault",
removeUrl: "/Home/RemoveDefault",
dialogAction: { closeOnComplete: true },
fileSelect: function (args) {
//Get the file from fileSelect event
uploadData["files"] = args["files"][0].rawFile; // for latest version
// if you are using older version 13.1.0.26 we suggest you to use args[0].rawFile;
var filename = args["files"][0].rawFile.name; // for latest version
// if you are using older version 13.1.0.26 we suggest you to use args[0].rawFile.name;
}
}); |