Call Uploadify from codebehind

117 Views Asked by At

I have two asp controls: a FileUpload and a Button On the FileUpload i add uploadify properties and it works I also want to use the button to do some operations on codebehind and THEN recall the File upload, but the file popup doesn't appear

For testing purpose i tried to reduce all on Client side, but it still won't work...

Here are my three tags

 <asp:Button Text="Browse" OnClick="Carica_File_Click" runat="server" ID="UploadButton" CssClass="Load_Button" align="center"/>
 <asp:Button Text="BrowseFromClient" OnClientClick="openfileDialog();" runat="server" ID="Button1" CssClass="Load_Button" align="center"/>
 <asp:FileUpload runat="server" ID="FileUploadMain" align="center" />

Client side

<script type="text/javascript">
    $(window).load(
       function () {
           $("#ContentPlaceHolderMain_FileUploadMain").uploadify({
               'swf': '../Scripts/uploadify.swf',
               'cancelImg': '../images/uploadify-cancel.png',
               'buttonText': 'Browse Files',
               'uploader': '../FrontEnd/Upload.ashx<%=GetUploadParams()%>',
                'folder': '',
                'fileDesc': 'UPLOAD FILES',
                'fileExt': '*.jpg;*.jpeg;*.gif;*.png;*.pdf;*.doc;*.docx',
                'multi': true,
                'auto': true,
                'buttonImage': '<%=GetUploadFileUrl()%>',
                'width': 217,
                'removeTimeout': 1,
                'removeCompleted': false,
                'buttonClass': '',
                'onUploadSuccess': function () {
                    location.reload(true);
                }

            });
        }
   );
</script>
<script>
        function openfileDialog() {
            $("#FileUploadMain").click();
        }
</script>

Server side

Protected Sub Carica_File_Click(sender As Object, e As EventArgs)
    Dim ClientID As String = FileUploadMain.ClientID
    ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", "document.getElementById('#" & ClientID & "').click();", True)
    ...code
End Sub

Where i go wrong?

1

There are 1 best solutions below

0
Progold SpA On

In the end i did a workaround:

for executing the code i forced a postback tag on the case the file was succesfully uploaded wich is then handled on the page_load on code behind

        $(window).load(
           function () {
               $("#ContentPlaceHolderMain_FileUploadMain").uploadify({
                    ....parameters
                   'onUploadSuccess': function (file, data, response) {
                       location.reload(true);
                       var ObjF = file.name;
                       __doPostBack('FileLoaded', ObjF);
                   }
               });
           }
       );

While for the click, as I understood is not possible for user's security (as an upload click handled by code or by hidden button could be dangerous) So i hid the uploadify button and put on it a customized button that i can handle

                <div style="position:relative;" class="row box_upload">
                    <div style="width:100%;">
                       <asp:FileUpload runat="server" ID="FileUploadMain" align="center" />
                    </div>
                    <asp:Button text="Browse" style="width:100%;height:100%;position:absolute;top:0;left:0;" runat="server" ID="UploadButton" return false;" ></asp:button>
                </div>

Cons are that with this i client click can't be used on the button as it's overlayed by the uploadify object click but i will leave it for when it will be necessary (and with the postback method i don't need anything else)