|
yogaboy -> RE: Document management (11/17/2005 19:20:31)
|
Thanks Bobby, looks very similar to the one on the link - that's helpful, means I have to think less! [:)] Here's what I've come up with, possibly a few holes but I'm certainly no expert on VB to C# (or even just c#!)
private void Page_Load(object sender, System.EventArgs e)
{
//put an if block around SendFile2Stream to satisfy your auth levels
//pass the querystring with the filename to the method
SendFile2Stream(Request.QueryString[0]);
}
//method for sending the file to the output stream
private void SendFile2Stream(string filename)
{
//declare local variables
FileStream fs = null;
byte [] bytBytes = null;
//try block
try
{
//open the filestream to the document
//note" Configuration.WebsiteDocsPath is a class-property I've got to get useful keys from the
//web.config
fs = new FileStream(Configuration.WebsiteDocsPath + filename, FileMode.Open, FileAccess.Read);
//create a byte array of doc's length
bytBytes = new byte[fs.Length];
//read the filestream into the array
fs.Read( bytBytes, 0, Convert.ToInt32( fs.Length ) );
}
//should be some catch blocks here!!!
//don't cross the streams! :)
finally
{
fs.Close();
}
//add the headers
Response.AddHeader( "Content-disposition", "attachment; filename=" + filename );
//pick a content type
Response.ContentType = "application/octet-stream";
//write the doc to the output stream
Response.BinaryWrite(bytBytes);
//end
Response.End();
}//end method
It worked for me so far...but I didn't add any exceptions yet as I'm sure there are lots and lots for the file/stream objects and I don't want to mislead anyone by leaving them out/just using the general exception. I also left out the authorization "if", as that can be sorted out according to one's own fascist desires (I've got a few of those).
|
|
|
|