簡體   English   中英

Javascript未在C#代碼中觸發

[英]Javascript Not Firing inside C# Code

我有以下代碼將附件上傳到程序中。 在此過程中,如果客戶端在先前在程序中上載的上載文件中選擇了相同的命名文檔,則必須向客戶端提供警報消息以更改文檔的名稱。

代碼段:

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代碼類型,但它們均無法正常工作。 代碼閱讀器僅讀取並傳遞代碼。

當我提交表單按鈕時,所有表單字段均被讀取並保存在一個對象中,當涉及上載部分時,將讀取上述代碼,並將clientFileName傳遞給filename對象並傳遞給sqlquery以便將其輸入數據庫。

對於客戶端更改文件名,它不會顯示任何警報彈出窗口。 因此,將相同的文件名傳遞給服務器,並且由於名稱相同而導致沖突開始。

謝謝。

僅當您重新發布網站時才會彈出警報。 它不能在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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM