-
Notifications
You must be signed in to change notification settings - Fork 0
/
Default.aspx.cs
89 lines (80 loc) · 3.13 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
using DevExpress.Web;
using DevExpress.XtraRichEdit;
public partial class _Default : Page {
protected void Page_Load(object sender, EventArgs e) {
if ( !String.IsNullOrEmpty(Request["loadFile"]) )
WritePdfToResponse(Request["loadFile"]);
}
private void WritePdfToResponse(string fileName) {
object uploadedFileBytes = Page.Session["UploadedFile"];
if ( uploadedFileBytes == null )
return;
MemoryStream stream = MemoryStreamHelper.FromBytes(uploadedFileBytes);
Page.Session["UploadedFile"] = null;
if ( stream == null )
return;
stream.WriteTo(Page.Response.OutputStream);
Page.Response.ContentType = "application/pdf";
Page.Response.HeaderEncoding = System.Text.Encoding.UTF8;
Page.Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.pdf", Path.GetFileNameWithoutExtension(fileName)));
Page.Response.End();
}
protected void ASPxUploadControl_FileUploadComplete(object sender, DevExpress.Web.FileUploadCompleteEventArgs e) {
ASPxUploadControl uploadControl = (ASPxUploadControl)sender;
UploadedFile uploadedFile = uploadControl.UploadedFiles[0];
e.CallbackData = Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty) + String.Format("?loadFile={0}", uploadedFile.FileName);
try {
Stream pdfStream = ConvertToPdf(uploadedFile.FileContent, uploadedFile.FileName);
Session["UploadedFile"] = MemoryStreamHelper.ToBytes(pdfStream);
} catch ( Exception ex ) {
e.ErrorText = ex.Message;
e.IsValid = false;
}
}
private Stream ConvertToPdf(Stream stream, string fileName) {
RichEditDocumentServer server = new RichEditDocumentServer();
server.LoadDocument(stream, FileExtensionHelper.GetDocumentFormat(fileName));
MemoryStream memoryStream = new MemoryStream();
server.ExportToPdf(memoryStream);
return memoryStream;
}
}
public static class MemoryStreamHelper {
public static byte[] ToBytes(Stream stream) {
stream.Position = 0;
byte[] buf = new byte[stream.Length];
stream.Read(buf, 0, (int)stream.Length);
return buf;
}
public static MemoryStream FromBytes(object bytes) {
byte[] buf = bytes as byte[];
MemoryStream stream = new MemoryStream(buf);
return stream;
}
}
public static class FileExtensionHelper {
private static Dictionary<string, DocumentFormat> useFormat = new Dictionary<string, DocumentFormat>();
static FileExtensionHelper() {
useFormat.Add("txt", DocumentFormat.PlainText);
useFormat.Add("docx", DocumentFormat.OpenXml);
useFormat.Add("doc", DocumentFormat.Doc);
useFormat.Add("rtf", DocumentFormat.Rtf);
useFormat.Add("odt", DocumentFormat.OpenDocument);
useFormat.Add("htm", DocumentFormat.Html);
useFormat.Add("mht", DocumentFormat.Mht);
useFormat.Add("epub", DocumentFormat.ePub);
}
public static DocumentFormat GetDocumentFormat(string fileName) {
DocumentFormat format;
string extension = Path.GetExtension(fileName).TrimStart('.').ToLower();
if ( String.IsNullOrEmpty(extension) )
return DocumentFormat.Undefined;
if ( useFormat.TryGetValue(extension, out format) )
return format;
return DocumentFormat.Undefined;
}
}