How to detect a cancel upload event

Last post 04-04-2012, 1:30 PM by Kenneth. 4 replies.
Sort Posts: Previous Next
  •  03-22-2012, 11:43 AM 73540

    How to detect a cancel upload event

    Hi all,
     
    My company is investigating the ajax uploader as a possible solution for our site.  Our requirements are that it needs to be able to use sqlfilestream on the backend with no temporary files on the server.  I've been able to get it working perfectly using a custom uploadprovider which copies the data off of the incoming stream onto a sql filestream.
     
    The problem that I'm running into is how to detect that a file upload is cancelled.  Because sql filestream runs within a transaction, I need to be able to roll back that transaction if the cancel uploads button is clicked on the client.  Does anyone know how to accomplish this?
     
    Some more details about my environment:
     
    Because the upload provider gets disposed and recreated for each chunk of data coming over the wire, I have a singleton class which controls all filestream operations.  In the upload provider - I pass along the correct arguments to the singleton, allowing it to perform the correct action.   Also, I'm not sure if it's relevant, but I have the client control set to manually start uploads.
     
    Based on what I have seen, it appears that the "Persist" function in the upload provider is equivalent to saying "The file upload is complete, go ahead and commit your transaction".   So that's where I've been performing the transaction commit.
     
    However, I assumed that the "unpersist" function would then be the equivalent of a "cancel", so that's where I attempted to put my transaction rollback code  -- But I can't find anything that actually calls the unpersist function.
     
    Clearly I'm missing something - can someone fill me in on:
    A) How to detect a cancel event in the upload provider
    and
    B) What are persist / unpersist actually for?  I'm having a hard time understanding what these methods are intended to do.
     
    Thanks!
     
    --Mike 
  •  03-23-2012, 7:51 AM 73544 in reply to 73540

    Re: How to detect a cancel upload event

    Hi mtaute,
     
    Please try the example below, it shows you how to catch the cancel event
    1. <%@ Page Language="C#" %>  
    2.   
    3. <%@ Register TagPrefix="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" Namespace="CuteWebUI" %>  
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    5. <html xmlns="http://www.w3.org/1999/xhtml">  
    6. <head id="Head1" runat="server">  
    7.     <title>example</title>  
    8. </head>  
    9. <body>  
    10.     <form id="form1" runat="server">  
    11.         <div>  
    12.             <CuteWebUI:Uploader runat="server" ID="Uploader1" CancelButtonID="ButtonCancel">  
    13.             </CuteWebUI:Uploader>  
    14.             <asp:Button ID="ButtonCancel" runat="server" Text="Cancel.." />  
    15.         </div>  
    16.     </form>  
    17.   
    18.     <script>  
    19.       
    20.     var btn1=document.getElementById('<%=ButtonCancel.ClientID %>');  
    21.     var int1;  
    22.     function MyClickHandler(event)  
    23.     {  
    24.         btn1.canclefunction();  
    25.       
    26.        alert("cancelled..")  
    27.           
    28.         //always cancel the onclick event, otherwise the form would do the postback!  
    29.         event=window.event||event;  
    30.         if(event.preventDefault)event.preventDefault();  
    31.         return event.returnValue=false;  
    32.     }  
    33.     function CheckUploaderScriptStatus()  
    34.     {  
    35.         //if the uploader script be loaded , the btn1.onclick would be setted  
    36.         //then you can replace it :  
    37.         if(btn1.onclick)  
    38.         {  
    39.             //stop the interval timer  
    40.             clearInterval(int1);  
    41.             //save the cancel handler  
    42.             btn1.canclefunction=btn1.onclick;  
    43.             //use my own handler  
    44.             btn1.onclick=MyClickHandler;  
    45.         }  
    46.     }  
    47.     int1=setInterval(CheckUploaderScriptStatus,10);  
    48.       
    49.     </script>  
    50.   
    51. </body>  
    52. </html>  
    Regards,
     
    Ken 
  •  03-23-2012, 10:35 AM 73546 in reply to 73544

    Re: How to detect a cancel upload event

    Thanks for replying Ken.
     
    I'm not a hardcore web developer, so I'm trying to translate what you've got to MVC3 / Razor which is what I'm using.  Please forgive me if I misunderstand :)
     
    It looks like you're capturing the cancel button event on the client side in javascript.  What I need is to know when the cancel button is clicked in the upload provider - on the server side.  My upload provider has access to a singleton class which is holding open resources like datastreams and sql connections.  I need to be able to release those resources if a cancel event is triggered.  Is some callback, whether to the provider or the upload handler ashx page which says "This upload was cancelled, please delete the temp file"?
     
    I can capture the event on the client side, so I suppose I could do it with a form post from the javascript code if I absolutely had to, but that seems like a bad design.
     
    Thanks again,
     
    --Mike
  •  04-03-2012, 6:56 PM 73604 in reply to 73546

    Re: How to detect a cancel upload event

    Can anyone help me with this?
     
    --Mike 
  •  04-04-2012, 1:30 PM 73612 in reply to 73604

    Re: How to detect a cancel upload event

    Hi mtaute,
     
    The cancel button click is the client side event.  If you must fire a server side event, then the only way is fire a server side event in the cancel click function, like the server side button click.
     
    Regards,
     
    Ken 
View as RSS news feed in XML