2012-03-05 16 views
1

我試圖讓客戶端上傳文件到我的服務器在ASP.NET C#網站。我創建了一個簡單的腳本來向我介紹上傳文件和創建腳本的細節,其中所有代碼(HTML和C#代碼)都位於同一個文件中。上傳文件腳本不會加載,因爲它找不到函數bt1Clicked()

我的問題:功能uploadFile()從不輸出/返回任何字符串結果時,它應該返回一個字符串描述如果上傳成功或失敗的原因。

我在做什麼錯?

PS:我已經更新我用下面的代碼的web.config:

<appSettings> 
<add key="folderPath" value="uploadedFiles"></add> 
</appSettings> 

PPS:這有什麼錯讓所有在同一.aspx文件我的C#代碼& HTML?將它們分離出來會更好嗎?

我的簡單代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication1.test" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script type="text/C#"> 
     using System; 
     using System.Collections.Generic; 
     using System.Linq; 
     using System.Web; 
     using System.Web.UI; 
     using System.Web.UI.WebControls; 
     using System.Configuration; 

     protected global::System.Web.UI.HtmlControls.HtmlInputFile fileUpload; 
     protected global::System.Web.UI.WebControls.Label lblMessage; 
     protected global::System.Web.UI.WebControls.Button btnSave; 
     protected global::System.Web.UI.HtmlControls.HtmlGenericControl test; 

     protected void bt1Clicked(Object sender, EventArgs e) 
     { 
      if (fileUpload.PostedFile != null) 
      { 
       string strFilename, strMessage; 
       strFilename = fileUpload.PostedFile.FileName.ToString(); 
       strMessage = uploadFile(strFilename, ConfigurationManager.AppSettings["folderPath"]); 
       output.InnerHtml = strMessage; 
      } 
     } 

     public string uploadFile(string fileName, string folderName) 
     { 
      if (fileName == "") 
      { 
       return "Invalid filename supplied"; 
      } 
      if (fileUpload.PostedFile.ContentLength == 0) 
      { 
       return "Invalid file content"; 
      } 
      fileName = System.IO.Path.GetFileName(fileName); 
      if (folderName == "") 
      { 
       return "Path not found"; 
      } 
      try 
      { 
       if (fileUpload.PostedFile.ContentLength <= 2048000) 
       { 
        fileUpload.PostedFile.SaveAs(Server.MapPath(folderName) + "\\" + fileName); 
        return "File uploaded successfully"; 
       } 
       else 
       { 
        return "Unable to upload,file exceeds maximum limit"; 
       } 
      } 
      catch (UnauthorizedAccessException ex) 
      { 
       return ex.Message + "Permission to upload file denied"; 
      } 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <input id="fileUpload" type="file" Runat="server" NAME="fileUpload"/> 
     <asp:button id="btnSave" runat="server" Text="Upload File" OnClick="bt1Clicked"></asp:button> 
     <p id="output" runat="server">Result should be displayed here after clicking</p> 
    </div> 
    </form> 
</body> 
</html> 

回答

1

如果你打算把你的C#代碼在頭版,你需要把它放在一個<script runat="server">標籤。當我這樣做時(這很少),我通常會在HTML之後放置<script>標籤。例如:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
... 
</html> 
<script runat="server"> 
//Code here 
</script> 

使用<script runat="server">的好處是,如果你正在編寫一個Web應用程序(其中的代碼編譯成一個DLL),你可以創建一個沒有做應用程序的一個新的版本上運行的服務器端頁面(當然,您必須從@Page指令中刪除CodeBehind屬性)。因此,如果您處於無論出於何種原因都無法進行新構建的情況,那麼在.aspx中爲所有頁面寫下所有內容都是非常有用的。這就是說,你可能最好在代碼隱藏文件中編寫代碼(在你的情況下,這是test.aspx.cs,你可以通過在解決方案資源管理器中右鍵點擊test.aspx並在VS.NET中獲得它,然後選擇「查看代碼」或在查看test.aspx文件時按F7)。使用代碼隱藏文件將代碼從HTML中分離出來,更重要的是編譯應用程序時會引發編譯錯誤。如果使用<script runat="server">,則在訪問頁面之前不會引發編譯錯誤。

1

回答您的PPS - 是的,一般認爲最好的做法是將C#代碼放在單獨的文件中。有此,我將列出一些在這裏爲你幾個原因:維修

  1. 簡單 - 有在其他文件中的C#使得它更容易修復或改變的事情以後
  2. 容易UI傢伙使用 - 如果你正在與一個設計型的人一起做你的HTML和CSS等,他們會更容易使用大多數的html ASPX標記,而不會讓所有的C#陷入困境
  3. 預編譯 - 如果您的C#代碼位於單獨的文件中,則它可以預編譯爲DLL,但如果它位於ASPX文件中,則需要在IIS運行時編譯它。
  4. 可能還有其他一些我沒有提到的原因......

有些人可能會說一個簡單的測試/教程應用程序,你不需要太擔心,但最好是在做教程等時練習最佳實踐,所以它對你來說並不陌生稍後的。

如果因爲某些原因必須將所有文件放在一個文件中,請將rsbarro的建議放在HTML下方的完全獨立的塊中,這樣很好,乾淨。

相關問題