从JavaScript或代码隐藏设置AjaxFileUpload控件的te值

本文关键字:控件 te AjaxFileUpload 设置 JavaScript 代码 隐藏 | 更新日期: 2023-09-27 18:29:22

我使用的是AjaxFileUpload控件,它运行得非常好。

我有单独的按钮,onClick需要将一个文件添加到AjaxFileUpload控件中,这样FileUploadControl就有了在按钮Click上选择的文件的值。

我正在尝试这样做,因为我希望附加到AjaxFileUpload控件的事件也能使用此按钮。

这是随附的代码

               <cc1:AjaxFileUpload ID="AjaxFileUpload3" runat="server" AllowedFileTypes="zip" Enabled="true"
                        MaximumNumberOfFiles="1" OnUploadComplete="File_Upload"  OnClientUploadComplete="UploadCompleteArchive" 
                        Width="240px" /> 
                    <asp:UpdatePanel runat="server">
                        <ContentTemplate>
                            <asp:HiddenField ID="hfBoxZip_URL" runat="server" />
                            <asp:HiddenField ID="hfBoxZip_Name" runat="server" />
                            <asp:Button ID="btnZip" runat="server" Text="Button" OnClick="btnZip_Click" style="display:none;"/>
                        </ContentTemplate>
                    </asp:UpdatePanel>

我需要使用JavaScript或CodeBehind(C#)的帮助。

感谢您的帮助。

从JavaScript或代码隐藏设置AjaxFileUpload控件的te值

好吧,经过更多的探索和尝试,我有了某种解决方案。

此解决方案的优点:

It allows user to upload a file and you can save it as any filename you want and still display it on screen
It works 

此解决方案的缺点:

This only seems to work with AsyncFileUpload...the same code does not work for AjaxFileUpload, unfortunately.
It requires forcing a postback so you get the "screen flashing" effect. Not elegant, but it works. 

解决方案直截了当。。。在javascript中,不需要查找文件名并将image.src设置为该文件名,只需强制回发updatepanel即可。这假设您已经在代码后面设置了ImageURL。

因此,完整的代码是:

在asp页面的头部:

<script type="text/javascript">
    function uploadComplete(sender, args) {
        __doPostBack('upTop', '');
    }
</script>

"upTop"是我的UpdatePanel 的ID

Body asp代码是标准的:

  <asp:UpdatePanel ID="upTop" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="Panel1" CssClass="panelbox" runat="server">
            <table>
                <tr>
                    <td>
                        <asp:Image ID="imgClient" runat="server" />
                    </td>
                    <td>
                        <ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" OnClientUploadComplete="uploadComplete" OnUploadedComplete="AsyncFileUpload1_UploadedComplete" ThrobberID="MyThrobber" runat="server" />
                        <asp:Image ID="MyThrobber" ImageUrl="~/Images/pleasewait.gif" Style="display: None" runat="server" />
                    </td>
                </tr>
            </table>
        </asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>
And finally the code-behind:
        Protected Sub AsyncFileUpload1_UploadedComplete(sender As Object, e As AjaxControlToolkit.AsyncFileUploadEventArgs)
        Dim Filepath As String
        Dim NewFileName As String
        Filepath = "/ClientPhotos"
        NewFileName = MapPath(Filepath) & "'" & CurrentCustomer.ID.ToString & ".jpg"
        Try
            AsyncFileUpload1.SaveAs(NewFileName)
        Catch ex As Exception
            ShowError("Error uploading photo: " & ex.Message)
            Exit Sub
        End Try
        Me.imgClient.ImageUrl = "~/ClientPhotos/" & CurrentCustomer.ID.ToString & ".jpg"
        Me.imgClient.Width = 200
        Me.imgClient.Height = 200
    End Sub

阅读更多-http://forums.asp.net/t/1894718.aspx?AjaxFileUpload+上传+和+显示+图像+