Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble writing a .doc or .txt file out to a web page...

Status
Not open for further replies.

VisualGuy

Programmer
May 27, 2003
162
US
What I'd like to do is when a user clicks on, say an anchor tag, that refers to a file that resides on a web server, they will either recieve a pop-up or be directed to a page that will diplay the contents of the .doc or .txt file within.

I'm sure there is a way, but I can't find any good examples and I'm thoroughly frustrated. Can someone supply me with a full model that shows how this would work?
 
a text file is simple enough. using a generic handler it would look something like this. We'll assume the file name is passed in via query string. all files are stored in a specific directory and the directory is within the virtual directory

example:
Code:
class DisplayFile : IHttpHandler
{
   public void ProcessRequest(HttpContext context)
   {
       var response = context.Response;
       var fileName = context.Request.QueryString["file"];
       var path = Server.MapPath("~/files/"+fileName);
       if(!File.Exists(path))
       {
           response.Redirect("MissingFile.aspx", false);
       }
       else
       {
           response.ContentType = DetermineTypeFrom(path);
           response.AppendHeader("Content-Disposition", "inline;filename="+path);
           response.Write(new FileStream(path));
       }
   }

   public bool IsReusable
   {
      get { return false; }
   }

   private string DetermineTypeFrom(string path)
   {
      //if text file return one thing,
      //if MS word return another.
   }
}
the method names in the else block may be incorrect, I'm writing this off the top of my head. this should display the document in the browser as the doc type.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks Jason, but what if I wanted it display as a .doc? Added to the problem, the web server doesn't have word on it. Would that matter?

I'm basically transferring these files, from a SQL Server BLOB field onto the web server, than I want to display it to a page when a user selects it from a list off of a web page.

ALSO, that model you attached:


Doesn't work... Also, where might I find more information on this subject?
 
If it is a blob column, you will have to stream the data to the page but Jason's example is still relevant. There are plenty of exampls on-line to do what you want to do.

As far as the link:
Code:
[URL unfurl="true"]http://site.com/DisplayFile.ashx?file=foo.txt[/URL]
That was just a sample link he used to show you how to pass the name of the file to the page in a querystring.
 
the details of how you send the file are irrelevant. it could be a file stream, a byte[] array, or text. What is important is that the ContentType match the data being sent in the response.

as jb said, there are plenty of examples of displaying binary (doc, jpg, bmp, pdf, etc) online. whether from a stream or a database.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Jason, would you mind sending me to one of these examples? I'm still confused.
 
here are plenty of choices:

here are two specific examples:

My only concern with the examples is they cram all the code into the asp.net implementation. I would separate concerns and have the data access in a separate class. Other than that the code works.

The only difference with these examples is you are loading text from a file, rather than an image from a database, but that doesn't really matter your streaming content. Writing the byte array and setting the proper content type are what is important.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top