2016-06-07 56 views
1

我有,我需要表现出一定的PDF文件和Word文档到用户的Web应用程序完全显示的网页浏览器。通过点击一个按钮的PDF是在浏览器窗口中完美呈现,但是当我这样做对.doc文件在下载看起来像这样.doc文件不使用C#

Result of the code below

我的代码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文件,而不失去其格式,因为服务器端代码可以在客户机上使用本地应用程序(如微软Word)不能打开该文件。是不是有可能写文件到输出流中的代码

回答

0

尝试两种不同的反应:myResp得,RESP输出。 见下文,这适用于我的机器开发和督促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); 
    } 
}