Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何打开Word文档而不必先将其保存在服务器上?_C#_Asp.net_Ms Word - Fatal编程技术网

C# 如何打开Word文档而不必先将其保存在服务器上?

C# 如何打开Word文档而不必先将其保存在服务器上?,c#,asp.net,ms-word,C#,Asp.net,Ms Word,通过使用合并字段为用户自定义模板,我从模板生成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, r

通过使用合并字段为用户自定义模板,我从模板生成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


谢谢。

使用神奇的
隔离存储文件

编辑:如何在不将文件保存到硬盘的情况下获得文件的永久副本,答案是-使用神奇的
隔离存储文件

(可以使用“神奇”一词)

我无法测试这段代码,也无法确定
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;
    }

重复:@Rikoshay这不是我的情况,因为我将处理大量文件。你能保存它,然后在下载后删除吗?@MikeSmithDev在它不工作后立即删除,因为它说“文件正在被另一个进程使用。”你能更准确地说吗?