C# .doc文件未在使用C的web浏览器中完美显示#

C# .doc文件未在使用C的web浏览器中完美显示#,c#,asp.net,filestream,C#,Asp.net,Filestream,我有一个web应用程序,其中我需要向用户显示一些pdf文件和word文档。通过单击按钮,pdf在浏览器窗口中呈现得非常完美,但当我为.doc文件执行此操作时,它会下载,如下所示 我的代码c#代码如下 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(FilePath); WebResponse myResp = myReq.GetResponse(); //R

我有一个web应用程序,其中我需要向用户显示一些pdf文件和word文档。通过单击按钮,pdf在浏览器窗口中呈现得非常完美,但当我为
.doc
文件执行此操作时,它会下载,如下所示

我的代码c#代码如下

            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(FilePath);
            WebResponse myResp = myReq.GetResponse();
            //Response.AddHeader("Content-Disposition", "inline; filename=\"" + FileName + "\"");
            Response.AppendHeader("Content-Disposition", "inline; filename=\"" + FileName + "\"");
            Response.ContentType = "application/msword";
            using (Stream stream = myResp.GetResponseStream())
            {
                int count = 0;
                do
                {
                    byte[] buf = new byte[10240];
                    count = stream.Read(buf, 0, 10240);
                    Response.OutputStream.Write(buf, 0, count);
                    Response.Flush();
                } while (stream.CanRead && count > 0);
            }

有没有办法在浏览器窗口中显示
doc/xls/ppt
文件而不丢失其格式,因为服务器端代码无法使用本机应用程序(如ms word)在客户端计算机中打开文件。难道不能像代码中那样将文件写入输出流吗?

尝试两种不同的响应:myResp to get和resp to output。 如下所示,这适用于我的机器dev和prod VS 2010

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(FilePath);
using (HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse()) {
    using (Stream stream = myResp.GetResponseStream()) {
    HttpResponse resp = HttpContext.Current.Response;
    resp.Clear();
    resp.ContentType = "Application/msword";
    resp.AddHeader("Content-Disposition", "attachment; filename=xyz.doc");
    int count = 0;
                do
                {
                    byte[] buf = new byte[10240];
                    count = stream.Read(buf, 0, 10240);
                    resp.OutputStream.Write(buf, 0, count);
                    resp.Flush();
                } while (stream.CanRead && count > 0);
    }
}

尝试两种不同的响应:myResp获取,resp输出。 如下所示,这适用于我的机器dev和prod VS 2010

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(FilePath);
using (HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse()) {
    using (Stream stream = myResp.GetResponseStream()) {
    HttpResponse resp = HttpContext.Current.Response;
    resp.Clear();
    resp.ContentType = "Application/msword";
    resp.AddHeader("Content-Disposition", "attachment; filename=xyz.doc");
    int count = 0;
                do
                {
                    byte[] buf = new byte[10240];
                    count = stream.Read(buf, 0, 10240);
                    resp.OutputStream.Write(buf, 0, count);
                    resp.Flush();
                } while (stream.CanRead && count > 0);
    }
}
您可以尝试使用Microsoft提供的服务。

您可以尝试使用Microsoft提供的服务