Wednesday, February 25, 2009

I was asked if I worked before on making a module to allow the users from downloading a document without the direct access to that document, the document (not just images) may be saved in SQL or on MOSS document library with no read access to the user.
Yes I worked before on file types like word files, but general files NO. and I give him the main idea of adding the document stream response and change the response content type.
And after a while I asked him about the output and he sends this code to me:



System.IO.FileInfo fileInfo = new System.IO.FileInfo(@"C:\d\AhmedCV.doc");
System.IO.FileStream fl = fileInfo.OpenRead();
byte[] file = new byte[(int)fl.Length];
fl.Read(file, 0, (int)fl.Length);
fl.Close();
System.IO.MemoryStream mem = new System.IO.MemoryStream(file);
Response.Clear();
Response.ContentType = "application/x-unknown";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
mem.WriteTo(Response.OutputStream);
Response.End();

No comments: