2014-02-09 87 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.