从JavaScript或代码背后设置Ajax文件上载控件的值

时间:2014-11-05 08:09:39

标签: javascript c# asp.net file-upload

我正在使用AjaxFileUpload Control,它完全正常。

我有单独的Button,onClick我需要将文件添加到AjaxFileUpload控件,以便FileUploadControl具有在ButtonClick上选择的文件的值。

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

这是附加的代码

               <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#)来帮助解决这个问题。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

好的,经过多次狩猎并尝试了一些事情,我得到了各种各样的解决方案。

此解决方案的优势:

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设置为该文件名,只需强制更新面板的回发即可。假设您已将ImageURL设置为代码隐藏。

因此,完整的代码是:

在asp页面的Head部分:

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

&#34; upTop&#34;是我的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+Upload+and+display+image+