-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHomeController.cs
46 lines (42 loc) · 1.44 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace RichMVC.Controllers
{
public class HomeController : Controller
{
private const string documentFolderPath = "~/Docs/";
public ActionResult Index()
{
ViewBag.Document = Convert.ToBase64String(System.IO.File.ReadAllBytes(Server.MapPath($"{documentFolderPath}template.docx")));
return View();
}
[HttpPost]
public ActionResult ExportDocument(string base64, string fileName, int format, string reason)
{
byte[] fileContent = Convert.FromBase64String(base64);
System.IO.File.WriteAllBytes(Server.MapPath($"{documentFolderPath}{fileName}.{GetExtension(format)}"), fileContent);
return new EmptyResult();
}
[HttpGet]
public JsonResult GetDataSource()
{
int id = 0;
var jsondata = new[] { "John", "Piter", "Mark" }.Select(name => new Hashtable() { { "FirstName", name }, { "Id", id++ } }).ToList();
return Json(jsondata, JsonRequestBehavior.AllowGet);
}
private static string GetExtension(int format)
{
switch (format)
{
case 4: return "docx";
case 2: return "rtf";
case 1: return "txt";
}
return "docx";
}
}
}