public void ProcessRequest(HttpContext context)
{
String sFullPath = String.Empty;
sFullPath = context.Request.QueryString["FullPath"];
if (sFullPath != null && sFullPath.Length > 4)
{
if (sFullPath.Contains(@"\"))
{
try
{
FileInfo _fileInfo = new FileInfo(sFullPath);
if (_fileInfo.Exists)
{
// Clear the whatever
context.Response.Buffer = false;
context.Response.Clear();
String sContentType = "";
// Determine the content type
switch (_fileInfo.Extension.ToLower())
{
case ".dwf":
sContentType = "Application/x-dwf";
break;
case ".pdf":
sContentType = "Application/pdf";
break;
case ".doc":
sContentType = "Application/vnd.ms-word";
break;
case ".ppt":
case ".pps":
sContentType = "Application/vnd.ms-powerpoint";
break;
case ".xls":
sContentType = "Application/vnd.ms-excel";
break;
default:
// Catch-all content type, let the browser figure it out
sContentType = "Application/octet-stream";
break;
}
// Set the headers ??? don't ask
context.Response.AddHeader("Content-Disposition", "filename=" + _fileInfo.Name);
context.Response.AddHeader("Content-Length", _fileInfo.Length.ToString());
// Set the content type
context.Response.ContentType = sContentType;
// Write the file to the browser
context.Response.WriteFile(sFullPath);
}
}
catch (Exception ex)
{
MBL.HMR.Equator_Portal.BusinessLogic.utilities.LogMessageToFile(ex.Message + "\r\n " + ex.StackTrace + "\r\n " + ex.InnerException);
}
}
}
}
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:
Sorry, my previous post came out upside down. Thanks a lot for the information you provided so far.
There is something else I forgot to mention, this code works on the deployment machines, as well as in debug mode (while I'm actually stepping through the code ie on localhost:3020//) BUT when I run the application from localhost// whether or not the project
configuration is debug or release it doesn't work at all. So identical code works on machines deployed to, but not on mine unless I am debugging the application.
Could this have something to do with ASPnet process access rights to files, because some filehandling code I wrote before worked in the dev environment but not in production.
I used all that streams and byte array stuff because I thought I could open the file as readonly, thereby avoiding any permissions issues, but that doesn't seem to make a difference. The end result is that I am now worried about deploying another build
to the users until I am sure this will keep working.
I don't think that checking to see if it's a return trip is the best approach for your DWF issues here, but I may have just misunderstood the issue. What you really need to do is figure out what is causing the stripped slashes and fix it (my guess is that
it's where you are using it in JavaScript, you should be replacing @"\" with @"\\" because just like C# it needs the slash escaped).
As for your file access problems, it is quite likely that your problem stems from the fact that ASP.NET does not have the appropriate file access to what ever you are trying to access.
Concerning opening the files in read-only mode to beat the access issues. You can only open a read-only version of a file if the permissions allow you to read the file in the first place.
Hi I am importing a vb6 dll in my .net web application and I am invoking a particular fuction from the dll, when the same is done in my windows application it works perfectly but it does not work in my web application. What should I possibly do to make
it work.
Please its quite urgent, it would be great if somebody could help on this
object fileName = myFilePath;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName,
ref missing,ref readOnly, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing);
oWordDoc.Activate();
I got this running on my machine, then on server i.e. if application run on server itself, it opens the doc, but whenever tried from client machine, the doc opens on server and not on client. Can you please help? Thank you.
Remove the "attachment; " portion of the "Content-Disposition" header and it should work. You should also send the proper mime type (text/html for HTML documents, for instance), but it is not always necessary and application/octet-stream basically lets
the browser figure it out. Testing on my own machine with various file types, it opened up the new window and either loaded the file in the window (text, HTML), or required me to open it with the Download dialog, which closed the window when I downloaded
the item. One case did not close the window, which was a PDF file using "application/octet-stream" but by providing "application/pdf" it was correctly opened in the window. I am running IE 7. Providing the mime type for the Office types (Excel's is "application/vnd.ms-excel"
I believe) did not seem to help, but I am running the beta for Office 2007, so that might be a security feature to not display them within the browser and it still closed the window.
Hi pickyh3d, I have follow ur suggestion to remove attachment, and it can read a pdf file with existing web form... but it cannot open a new window or new broswer... please help
reckface
Member
17 Points
13 Posts
Re: Opening a Word or PDF file from a webpage. C#, VS2005, APS 2.0
Feb 09, 2007 03:04 PM|LINK
public void ProcessRequest(HttpContext context) { String sFullPath = String.Empty; sFullPath = context.Request.QueryString["FullPath"]; if (sFullPath != null && sFullPath.Length > 4) { if (sFullPath.Contains(@"\")) { try { FileInfo _fileInfo = new FileInfo(sFullPath); if (_fileInfo.Exists) { // Clear the whatever context.Response.Buffer = false; context.Response.Clear(); String sContentType = ""; // Determine the content type switch (_fileInfo.Extension.ToLower()) { case ".dwf": sContentType = "Application/x-dwf"; break; case ".pdf": sContentType = "Application/pdf"; break; case ".doc": sContentType = "Application/vnd.ms-word"; break; case ".ppt": case ".pps": sContentType = "Application/vnd.ms-powerpoint"; break; case ".xls": sContentType = "Application/vnd.ms-excel"; break; default: // Catch-all content type, let the browser figure it out sContentType = "Application/octet-stream"; break; } // Set the headers ??? don't ask context.Response.AddHeader("Content-Disposition", "filename=" + _fileInfo.Name); context.Response.AddHeader("Content-Length", _fileInfo.Length.ToString()); // Set the content type context.Response.ContentType = sContentType; // Write the file to the browser context.Response.WriteFile(sFullPath); } } catch (Exception ex) { MBL.HMR.Equator_Portal.BusinessLogic.utilities.LogMessageToFile(ex.Message + "\r\n " + ex.StackTrace + "\r\n " + ex.InnerException); } } } }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:
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:
reckface
Member
17 Points
13 Posts
Re: Opening a Word or PDF file from a webpage. C#, VS2005, APS 2.0
Feb 09, 2007 03:14 PM|LINK
Sorry, my previous post came out upside down. Thanks a lot for the information you provided so far.
There is something else I forgot to mention, this code works on the deployment machines, as well as in debug mode (while I'm actually stepping through the code ie on localhost:3020//) BUT when I run the application from localhost// whether or not the project configuration is debug or release it doesn't work at all. So identical code works on machines deployed to, but not on mine unless I am debugging the application.
Could this have something to do with ASPnet process access rights to files, because some filehandling code I wrote before worked in the dev environment but not in production.
I used all that streams and byte array stuff because I thought I could open the file as readonly, thereby avoiding any permissions issues, but that doesn't seem to make a difference. The end result is that I am now worried about deploying another build to the users until I am sure this will keep working.
Any ideas?
Thanks
pickyh3d
Star
9696 Points
1955 Posts
Re: Opening a Word or PDF file from a webpage. C#, VS2005, APS 2.0
Feb 09, 2007 04:09 PM|LINK
I don't think that checking to see if it's a return trip is the best approach for your DWF issues here, but I may have just misunderstood the issue. What you really need to do is figure out what is causing the stripped slashes and fix it (my guess is that it's where you are using it in JavaScript, you should be replacing @"\" with @"\\" because just like C# it needs the slash escaped).
As for your file access problems, it is quite likely that your problem stems from the fact that ASP.NET does not have the appropriate file access to what ever you are trying to access.
Concerning opening the files in read-only mode to beat the access issues. You can only open a read-only version of a file if the permissions allow you to read the file in the first place.
spdy_mn
Member
4 Points
3 Posts
Re: Opening a Word or PDF file from a webpage. C#, VS2005, APS 2.0
Nov 14, 2008 04:27 PM|LINK
Hi this article was useful but I have an issue if somebody could help that would be great
spdy_mn
Member
4 Points
3 Posts
Re: Opening a Word or PDF file from a webpage. C#, VS2005, APS 2.0
Nov 14, 2008 04:35 PM|LINK
Hi I am importing a vb6 dll in my .net web application and I am invoking a particular fuction from the dll, when the same is done in my windows application it works perfectly but it does not work in my web application. What should I possibly do to make it work.
Please its quite urgent, it would be great if somebody could help on this
RamKumars
Member
20 Points
29 Posts
Need Souce Code for find duplicate Images from a webpage. C#, VS2005
Apr 20, 2009 11:45 AM|LINK
I Need Souce Code for Find Duplicate Images from Server or Server Folder
Reply me
Regards
L.Prem
saranahibi
Member
4 Points
2 Posts
Re: Opening a Word or PDF file from a webpage. C#, VS2005, APS 2.0
May 31, 2009 08:16 AM|LINK
object fileName = myFilePath;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName,
ref missing,ref readOnly, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing);
oWordDoc.Activate();
I got this running on my machine, then on server i.e. if application run on server itself, it opens the doc, but whenever tried from client machine, the doc opens on server and not on client. Can you please help? Thank you.
JunzTan
Member
4 Points
16 Posts
Re: Opening a Word or PDF file from a webpage. C#, VS2005, APS 2.0
Aug 03, 2012 03:40 AM|LINK
Hi pickyh3d, I have follow ur suggestion to remove attachment, and it can read a pdf file with existing web form... but it cannot open a new window or new broswer... please help