Reputation:
I am generating word documents from a template, by customizing it for the user using merge field. For example: Name: "User's name". Right now I save the documents on the server using
object miss = System.Reflection.Missing.Value;
object oDocName = "C:\\Users\\admin\\Desktop\\test002.doc";
wordDoc.SaveAs(ref oDocName, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
((_Application)wordApp).Quit(ref miss, ref miss, ref miss);
then, i make the document available for download:
protected void download_Click(object sender, EventArgs e)
{
FileStream fileStream = new FileStream("C:\\Users\\ebimbari\\Desktop\\test002.doc", FileMode.Open);
int fileSize = (int)fileStream.Length;
byte[] Buffer = new byte[fileSize];
fileStream.Read(Buffer, 0, fileSize);
fileStream.Close();
Response.ContentType = ContType("C:\\Users\\admin\\Desktop\\test002.doc");
Response.BinaryWrite(Buffer);
Response.AddHeader("content-disposition", "attachment;filename=\"" + "form" + ".doc");
Response.End();
}
The problem is that I don't want the form to be saved on the server. Can I directly open the document of make it avaiable for download? I'm using Microsoft.Office.Interop.Word
Thanks.
Upvotes: 1
Views: 2337
Reputation: 706
Use the magical IsolatedStorageFile
EDIT: The answer to 'how to have a persistent copy of a file, without actually saving it to the hard-disk', is - Use the magical IsolatedStorageFile
.
(The word 'magical' may be ommited).
I can't test this code, and not sure how worddoc.SaveAs(...)
behaves. Please test and let me know:
private static readonly string CACHE = @"MyWordCache";
static void PutFile(object wordDoc, string fname)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.DirectoryExists(CACHE))
store.CreateDirectory(CACHE);
object fullname = Path.Combine(CACHE, fname);
wordDoc.SaveAs(ref fullname, ...);
}
}
protected bool Downloadfile(string fname)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.DirectoryExists(CACHE))
return false;
var fullname = Path.Combine(CACHE, fname);
using (var fileStream = store.OpenFile(fullname, FileMode.Open))
{
int fileSize = (int)fileStream.Length;
byte[] Buffer = new byte[fileSize];
fileStream.Read(Buffer, 0, fileSize);
Response.ContentType = ContType(fullname);
Response.BinaryWrite(Buffer);
Response.AddHeader("content-disposition", "attachment;filename=\"" + "form" + ".doc");
Response.End();
}
}
return true;
}
Upvotes: 2