简体   繁体   English

从JavaScript或后台代码设置AjaxFileUpload控件的值

[英]Setting te Value of AjaxFileUpload Control from JavaScript or Code Behind

I am using AjaxFileUpload Control, and it works perfectly fine. 我正在使用AjaxFileUpload Control,它工作得很好。

I have separate Button, onClick of which I need to add a File to the AjaxFileUpload Control, so that the FileUploadControl has the Value of the File which is Selected on ButtonClick. 我有一个单独的Button,需要将onClick的onClick添加到AjaxFileUpload控件中,以便FileUploadControl具有在ButtonClick上选择的文件的值。

I am trying to do this as I want events attached to AjaxFileUpload Control to work with this button as well. 我正在尝试执行此操作,因为我希望附加到AjaxFileUpload Control的事件也可以与此按钮一起使用。

Here is the code attached 这是附加的代码

               <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>

I need help with this using JavaScript or CodeBehind (C#). 我需要使用JavaScript或CodeBehind(C#)进行帮助。

Any help is appreciated. 任何帮助表示赞赏。

Ok, after some more hunting around and trying out a few things, I've got a solution of sorts. 好吧,经过更多的探索并尝试了一些方法之后,我有了各种各样的解决方案。

Upside of this solution: 该解决方案的优势:

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 

Downside of this solution: 该解决方案的缺点:

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. 

The solution is straight-forward...In the javascript, rather than finding the filename and setting the image.src to that filename, simply force a postback of the updatepanel. 解决方法很简单...在javascript中,无需查找文件名并将image.src设置为该文件名,只需强制执行updatepanel的回发即可。 This assumes you have set the ImageURL in the code-behind. 假设您已在后面的代码中设置了ImageURL。

Thus, the complete code is: 因此,完整的代码是:

In the Head section of the asp page: 在asp页面的Head部分中:

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

"upTop" is the ID of my UpdatePanel “ upTop”是我的UpdatePanel的ID

The Body asp code is standard: 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

Read More - http://forums.asp.net/t/1894718.aspx?AjaxFileUpload+Upload+and+display+image+ 阅读更多-http://forums.asp.net/t/1894718.aspx?AjaxFileUpload+Upload+and+display+image+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM