2014-02-09 155 views
1

我有以下代碼來上傳程序中的附件。在此過程中,如果客戶端在程序中先前上傳的上傳文件中選擇了相同的已命名文檔,則必須給客戶端提供警告消息以更改文檔的名稱。C#代碼內未觸發Javascript代碼

代碼段:

private string UploadFile() 
    { 
     string pathToSaveFile = Server.MapPath("~/Data/"); 
     string clientFileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName); 

     string upload_data = pathToSaveFile + clientFileName; 

     if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0) 
     { 

      if (System.IO.File.Exists(upload_data)) 
      { 
       //using Response.write 
       Response.Write(@"<script type='text/javascript'>alert('Rename it please.');</script>"); 

       //ClientScriptManager 
       var clientScript = Page.ClientScript; 
       clientScript.RegisterClientScriptBlock(this.GetType(), "AlertScript", "alert('Rename it please.')'", true); 

       //ScriptManger 
       ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('Member Registered Sucessfully');", true); 

      } 
      else 
      { 
       FileUpload1.PostedFile.SaveAs(System.IO.Path.Combine(pathToSaveFile, clientFileName)); 
      } 
     } 
     else 
     { 
      Response.Write("Not Available"); 
     } 

     return clientFileName; 
    } 

我已經使用了JavaScript的類型代碼的程序,但他們都無法正常工作。代碼閱讀器只是讀取代碼並通過它。

當我提交表單b utton時,所有表單字段都被讀取並保存在一個對象中,當涉及上傳部分時,上面的代碼被讀取並且clientFileName被傳遞給文件名對象並傳遞給sqlquery將其輸入到數據庫中。

它不顯示任何警報彈出窗口爲客戶端更改文件的名稱。因此,相同的文件名稱被傳遞給服務器,並且因相同的名稱而開始衝突。

謝謝。

+0

你在哪裏調用從這個私有方法?您的頁面是否在POST/Submit上進行完整刷新? –

+0

當我們點擊表單提交按鈕Button1_Click。它讀取所有形式的文本文本,最後到達上傳選項,然後我將上述過程保存在名爲UploadFile()的函數中; – Jatin

+0

UploadFile()返回上傳文件名,名稱保存在對象中並傳遞給sql查詢以進行數據庫輸入。問題是警報不會彈出。 – Jatin

回答

0

僅當您重新發佈網站時纔會彈出提醒。 它不能在C#代碼中工作。

請參見下面的代碼(僅供參考):

private bool UploadFile() 
{ 
    string pathToSaveFile = Server.MapPath("~/Data/"); 
    string clientFileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName); 

    string upload_data = pathToSaveFile + clientFileName; 

    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0) 
    { 

     if (System.IO.File.Exists(upload_data)) 
     { 
      //using Response.write 
      Response.Write(@"<script type='text/javascript'>alert('Rename it please.');</script>"); 

      //ClientScriptManager 
      var clientScript = Page.ClientScript; 
      clientScript.RegisterClientScriptBlock(this.GetType(), "AlertScript", "alert('Rename it please.')'", true); 

      //ScriptManger 
      ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('Member Registered Sucessfully');", true); 
    return false; 
     } 
     else 
     { 
      FileUpload1.PostedFile.SaveAs(System.IO.Path.Combine(pathToSaveFile, clientFileName)); 
     } 
    } 
    else 
    { 
     Response.Write("Not Available"); 
    } 

ViewState["FileName"] = clientFileName; 
    return true; 
} 

以外的功能使用是這樣的:

if(UploadFile()) 
     //run your SQL queue 
    else 
     return;//do all the return that will repost the website.