1 public void ProcessRequest(HttpContext context)
2 {
3 String sFullPath = String.Empty;
4 sFullPath = context.Request.QueryString["FullPath"];
5 if (sFullPath != null && sFullPath.Length > 4)
6 {
7 if (sFullPath.Contains(@"\"))
8 {
9 try
10 {
11 FileInfo _fileInfo = new FileInfo(sFullPath);
12 if (_fileInfo.Exists)
13 {
14 // Clear the whatever
15 context.Response.Buffer = false;
16 context.Response.Clear();
17 String sContentType = "";
18 // Determine the content type
19 switch (_fileInfo.Extension.ToLower())
20 {
21 case ".dwf":
22 sContentType = "Application/x-dwf";
23 break;
24 case ".pdf":
25 sContentType = "Application/pdf";
26 break;
27 case ".doc":
28 sContentType = "Application/vnd.ms-word";
29 break;
30 case ".ppt":
31 case ".pps":
32 sContentType = "Application/vnd.ms-powerpoint";
33 break;
34 case ".xls":
35 sContentType = "Application/vnd.ms-excel";
36 break;
37 default:
38 // Catch-all content type, let the browser figure it out
39 sContentType = "Application/octet-stream";
40 break;
41 }
42 // Set the headers ??? don't ask
43 context.Response.AddHeader("Content-Disposition", "filename=" + _fileInfo.Name);
44 context.Response.AddHeader("Content-Length", _fileInfo.Length.ToString());
45 // Set the content type
46 context.Response.ContentType = sContentType;
47 // Write the file to the browser
48 context.Response.WriteFile(sFullPath);
49 }
50 }
51 catch (Exception ex)
52 {
53 MBL.HMR.Equator_Portal.BusinessLogic.utilities.LogMessageToFile(ex.Message + "\r\n " + ex.StackTrace + "\r\n " + ex.InnerException);
54 }
55 }
56 }
57 }
Yup! I agree. I have never done anything similar before, so I've got lots of trial and error code in there that I still hadn't removed. But I have implemented your suggestions and quess what... it works. I suppose the surprising thing is the fact that Response.WriteFile worked. I didn't expect that to work, because I had tried similar before and it just wrote garbled text to the screen.
Interestingly though, I have now noticed that office application documents work, but prompt first which is a user preference, pdfs work exactly the way I want, no prompt, it just shows the file.
dwf files on the other hand don't. The ProcessRequest method is called twice and the second time it's called the dwf file name has subtracted all the back slashes. So if I start as c:\\my documents\\test.dwf as the filename in the query string, I end up with c:\my documents\test.dwf the first time, and c:my documentstest.dwf the second time. I know the dwf viewer currently doesn't work reliably in internet explorer (mine doesn't work at all) but this is even more unusual.
As a work around I now quadruple the "\" character since I am passing this query string from a dynamically generated javascript:
1 _visibleJscript.Append("ShowMeHideMe('" + drawingDiv.ClientID + "', '" + GridViewDiv.ClientID + "'); ");
2 _visibleJscript.Append("document.getElementById('" + PreviewBox.ClientID + @"').src= '"
3 + Request.ApplicationPath + "/preview.ashx?FullPath="
4 + e.Row.Cells[9].Text.Replace(@"\", @"\\\\") + "';");
5 e.Row.Cells[9].Text = "Image";
6 e.Row.Cells[9].CssClass = "portalbutton";
7 e.Row.Cells[9].Attributes["onclick"] = _visibleJscript.ToString();
Any ideas how to check if the ProcessRequest is running for the first time, like an "isPostBack" type variable?
Thanks
Oh here's the updated ProcessRequest: