C# Word、excel文件不显示在浏览器中

C# Word、excel文件不显示在浏览器中,c#,excel,pdf,C#,Excel,Pdf,下面的代码在我的.cs文件中 受保护的无效btnView\u Clickobject发件人,事件参数e { ViewFile.ashx代码 公共void ProcessRequest HttpContext上下文 { bool freeddownload=true string supportingFile = context.Request.QueryString["Name"].ToString(); string strpath = HttpContext.Current.

下面的代码在我的.cs文件中

受保护的无效btnView\u Clickobject发件人,事件参数e {

ViewFile.ashx代码

公共void ProcessRequest HttpContext上下文 {

bool freeddownload=true

    string supportingFile = context.Request.QueryString["Name"].ToString();
    string strpath = HttpContext.Current.Server.MapPath(supportingFile);
    string strname = Path.GetFileName(strpath);
    string strextension = Path.GetExtension(strpath);
    string strtype = "";

    if (strextension != null)
    {
        switch (strextension.ToLower())
        {
            case ".htm":
            case ".html":
                strtype = "text/HTML";
                break;

            case ".txt":
                strtype = "text/plain";
                break;
            case ".doc":
                strtype = "application/msword";
                break;
            case ".rtf":
                strtype = "application/msword";
                break;
            case ".docx":
                strtype ="application/vnd.openxmlformats-officedocument.wordprocessingml.document" ; //"application/msword";
                break;
            case ".xls":
                strtype = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                strtype = "application/vnd.ms-excel";
                break;
            case ".pdf":
                strtype = "Application/pdf";
                break;
        }
    }
    if (freeDownload)
    {
       // context.Response.AppendHeader("content-disposition", "inline: filename=\"" + strname + "\"");
    }
    if (strtype != null)
    {
        FileInfo file = new FileInfo(strpath);
        context.Response.ContentType = strtype;
        context.Response.AddHeader("Content-Disposition", "inline; filename=\"" + file.Name + "\"");
        context.Response.AddHeader("Content-Length", file.Length.ToString());
        context.Response.TransmitFile(file.FullName);
        context.Response.WriteFile(strpath);
        context.Response.Flush();
        context.Response.End();

    }

}

请任何人帮助我?当我单击“查看”按钮时,如果它是pdf文件,它将显示在浏览器中。但是如果它是.docx,doc,.xlsx,…它将直接下载。如何在浏览器中显示这些文件?

您可以在按钮上单击“创建文件夹浏览器”对话框,将其筛选到相关的扩展名,然后将其显示给用户,如下所示:

FolderBrowserDialog browseDialog;

browseDialog.Filter=" Wordfile (*.dotm; *.dot; *.docx; *.dotx; *.doc; *.docm; *.rtf; *.txt)|*.dotm; *.dot; *.docx; *.dotx; *.doc; *.docm; *.rtf; *.txt";  

if (browseDialog.ShowDialog() == DialogResult.OK)
    {
       MessageBox.Show( System.IO.Path.GetFullPath(browseDialog.FileName));
    }

谢谢你的回复。你能解释一下如何在web应用程序中使用folderbrowserDialog吗?我想你的代码是windows应用程序。如果我使用System.windows.Form,我会遇到一些错误。因为我使用web控件。我需要web应用程序的解决方案Application@user3300574,是的,这是WinForm语法,我建议您将web应用程序的标记添加到这篇文章,以便这一领域的专家能够帮助你。当你设法解决它时,请发布答案。谢谢。
FolderBrowserDialog browseDialog;

browseDialog.Filter=" Wordfile (*.dotm; *.dot; *.docx; *.dotx; *.doc; *.docm; *.rtf; *.txt)|*.dotm; *.dot; *.docx; *.dotx; *.doc; *.docm; *.rtf; *.txt";  

if (browseDialog.ShowDialog() == DialogResult.OK)
    {
       MessageBox.Show( System.IO.Path.GetFullPath(browseDialog.FileName));
    }