2014-06-30 67 views
0

以下代碼位於我的.cs文件中。單詞,excel文件不在瀏覽器中顯示

保護無效btnView_Click(對象發件人,EventArgs的) {

string strurl = "ViewFile.ashx?Name=Img/FT.pdf";//Welcome.docx"; 
    string StrPop = "window.open('"+strurl+"', '_newtab')"; 
    ScriptManager.RegisterClientScriptBlock(sender as Control, this.GetType(), "OpenWindow", StrPop, true); 
} 

ViewFile.ashx代碼。

公共無效的ProcessRequest(HttpContext的上下文) {

布爾freeDownload = 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,..它會直接下載。我怎樣才能在瀏覽器中顯示該文件?

回答

0

您可以點擊鏈接創建的FolderBrowserDialog,給它過濾到相關的擴展名,然後展示給用戶,這樣的:

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)); 
    } 
+0

感謝您的答覆。你能解釋一下如何在webapplicaion.i中使用folderbrowserDialog,你的代碼是windows應用程序。如果我去使用System.Windows.Form我有一些錯誤。因爲我使用web controls.i需要web應用程序的解決方案 – user3300574

+0

@ user3300574,是的,它是WinForm的語法,我建議你在帖子中添加'web application'標籤,以便這個領域的專家可以幫助你。請在設法解決問題時發佈答案。謝謝。 – user3165438

相關問題