-
Notifications
You must be signed in to change notification settings - Fork 2
/
Default.aspx.cs
50 lines (46 loc) · 1.6 KB
/
Default.aspx.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
47
48
49
50
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RichWebForms
{
public partial class Default : System.Web.UI.Page
{
public string InitialDocument;
private const string documentFolderPath = "~/App_Data/";
protected void Page_Load(object sender, EventArgs e)
{
InitialDocument = Convert.ToBase64String(System.IO.File.ReadAllBytes(
Server.MapPath($"{documentFolderPath}template.docx")));
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static IEnumerable<Hashtable> GetDataSource()
{
int id = 0;
return new[] { "John", "Piter", "Mark" }.Select(name => new Hashtable() { { "FirstName", name }, { "Id", id++ } }).ToList();
}
[WebMethod]
public static void Export(string base64, string fileName, int format, string reason)
{
byte[] fileContent = Convert.FromBase64String(base64);
string path = System.Web.Hosting.HostingEnvironment.MapPath($"{documentFolderPath}{fileName}.{GetExtension(format)}");
System.IO.File.WriteAllBytes(path, fileContent);
}
private static string GetExtension(int format)
{
switch (format)
{
case 4: return "docx";
case 2: return "rtf";
case 1: return "txt";
}
return "docx";
}
}
}