AjaxControlToolkit 的异步上传控制

本文关键字:控制 异步 AjaxControlToolkit | 更新日期: 2023-09-27 17:56:27

>我正在使用AjaxControlToolkit的asyncupload控件,我想检查要更新的文件是否已经存在或不在服务器上。我们该怎么做呢?请帮帮我。

<!-- Client side code for control-->
<script>
 function uploadError(sender, args) {
        //document.getElementById('lblStatus').innerText = args.get_fileName(), "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
    }
    function StartUpload(sender, args) {                
        var nodeSelectedText = document.getElementById('<%=lblFileLocation1.ClientID%>').innerHTML;
        if (nodeSelectedText == "") {            
            $("#msgMissingSelection").dialog("open");
            args.set_cancel(true);
        }
        else {
            return true;
        }        
    }
        function UploadComplete(sender, args) {     
            var hdnFieldVal = document.getElementById('<%=hdnField.ClientID%>');
            if(hdnFieldVal.value == "1")
{
            $("#msgFileUploadExists").dialog("open");        
}
            else
            {
            $("#msgFileUpload").dialog("open");        
}
        }
</script>

<!-- Control Code in aspx-->
<tr>
                            <td></td>
                            <td> 
                                <cc1:AsyncFileUpload ID="FileUpload2"  Width="265px" runat="server" 
                                    OnClientUploadError="uploadError" 
                                    OnClientUploadStarted="StartUpload"
                                    OnClientUploadComplete="UploadComplete"
                                    CompleteBackColor="Lime" UploaderStyle="Modern" 
                                    ErrorBackColor="Red" 
                                    ThrobberID="Throbber" 
                                    onuploadedcomplete="AsyncFileUpload1_UploadedComplete" 
                                    UploadingBackColor="#66CCFF" />
                                    <asp:Label ID="Throbber" runat="server" Style="display: none">
                                        <img src="../../images/indicator.gif"  align="absmiddle" alt="loading" />
                                    </asp:Label> 
                                    <asp:HiddenField ID="hdnField" runat="server" value=""/>
                            </td>
                        </tr>
/

/.CS 用于上传控制的代码

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
string filePath="C:'Documents'temp.txt"
         if(File.Exists(filePath))
{
hdnField.value="1";//Not able to access this value
}
}

谢谢!!

AjaxControlToolkit 的异步上传控制

您只需

要在客户端脚本中设置隐藏字段值。

请参阅我对服务器端代码进行了一些更改:

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        string filePath = "C:''Documents''temp.txt";
        string hiddenValue = "0";
        if (File.Exists(filePath))
        {
            hiddenValue = "1";
        }
        //This script will set required value for hidden field.
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "SetHiddenField", String.Format("top.$get('{0}').value = '{1}'", hdnField.ClientID, hiddenValue), true);
    }

在脚本位置进行更改,如下所示:

 <ajaxToolkit:ToolkitScriptManager runat="Server" EnablePartialRendering="true" ID="ScriptManager1" />
       <script>
 function uploadError(sender, args) {
        //document.getElementById('lblStatus').innerText = args.get_fileName(), "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
    }
    function StartUpload(sender, args) {                
        var nodeSelectedText = document.getElementById('<%=lblFileLocation1.ClientID%>').innerHTML;
        if (nodeSelectedText == "") {            
            $("#msgMissingSelection").dialog("open");
            args.set_cancel(true);
        }
        else {
            return true;
        }        
    }
        function UploadComplete(sender, args) {     
            var hdnFieldVal = document.getElementById('<%=hdnField.ClientID%>');
            if(hdnFieldVal.value == "1")
            {
              $("#msgFileUploadExists").dialog("open");        
            }
            else
            {
              $("#msgFileUpload").dialog("open");        
            }
        }
</script>

使用以下方法检查文件是否存在:-

文件.存在

您可以根据文件名进行比较。