我正在使用合并字段为用户自定义word文档。例如:名称:“用户名”。现在我使用
将文档保存在服务器上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);
然后,我可以下载文件:
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();
}
问题是我不希望将表单保存在服务器上。我可以直接打开可下载的文件吗?我正在使用 Microsoft.Office.Interop.Word
感谢。
答案 0 :(得分:2)
使用神奇的IsolatedStorageFile
编辑:“如何拥有文件的永久副本而不实际将其保存到硬盘”的答案是 - 使用神奇的IsolatedStorageFile
。
(可以省略'魔法'这个词)。
我无法测试此代码,也不确定worddoc.SaveAs(...)
的行为方式。请测试并告诉我:
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;
}