views:

316

answers:

2

Uploadify is a jQuery/Flash plugin for uploading multiple files. It's working great, except I can't figure out how trigger e-mail when all files are complete. If I try to add something like <% SendEmail(); %> to the onAllComplete parameter, it just sends the e-mail when the page loads.

Is there a way to do this within the handler recommended here or from this post? Or is there some way to trigger a post in the onAllComplete parameter?

<script type="text/javascript">
    // <![CDATA[
    var FirstName = $('[id$=HiddenField4]').val();
    var MiddleName = $('[id$=HiddenField5]').val();
    var ClientName = $('[id$=HiddenField6]').val();
    var queueSize = $('#fileInput').uploadifySettings('queueSize');
    $(document).ready(function() {
        $('#fileInput').uploadify({
            'uploader': 'scripts/uploadify/uploadify.swf',
            'script': 'Upload.ashx',
            'scriptData': { 'first': FirstName, 'middle': MiddleName, 'client': ClientName, 'queueSize': queueSize },
            'cancelImg': 'scripts/uploadify/cancel.png',
            'auto': true,
            'multi': true,
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg;*.pdf',
            'queueSizeLimit': 90,
            'sizeLimit': 10000000,
            'buttonText': 'Upload Documents',
            'folder': '/uploads',
            'onComplete': function(event, queueID, fileObj, response, data) {
                 alert(response);
            },
            'onAllComplete': function(event, queueID, fileObj, response, data) {
                 <% SendEmail(); %>
            },
            'buttonImg': 'images/upload.png'
         });
      });
      // ]]></script>

I've also tried queueSize declaring it as

var queueSize = $(".uploadifyQueueItem").size();

queueSize always posts as 0 when I debug my handler.

+1  A: 

You could put the instruction to send the E-Mail into the script that receives the uploaded file (Upload.ashx in your case.)

That file will be called when the upload has been finalized.

An alternative would be making an Ajax call in the onComplete callback, calling another ashx script that sends out the E-Mails. Anyway, there is no JavaScript way of sending out the E-Mail, you will have to do that on the server side.

Pekka
The handler is executed once for every file. I need to send e-mail when all files (1 or more) are complete.
David
@David I see. Then check out the last paragraph I just added.
Pekka
I understand there is no way to send e-mail client side, but I didn't know I could force an AJAX post from javascript. Should I look at jQuery.ajax() ?
David
@David yup, if you're running jQuery already then that is the nicest way. (See BalusC's answer for an example.)
Pekka
awesome! thanks! Is it possible to post to .ashx? I did a .ajax() post to email.aspx and the page_load just sends the e-mail and dies.
David
@David should be no problem - what dies and at what point?
Pekka
Sorry wasn't clear. I was informing you what I coded up. I should've said "Page_Load completes e-mail successfully and has given me tons of new ideas for jQuery ajax postbacks" :) I'm only familiar with .NET handler classes since I found this plugin, so I guess I'll have to read up no them
David
+1  A: 

You need to solve this at the server side. But Uploadify is an entirely client side script (JS+Flash). You need to write/invoke the mailing code in the server side which get invoked by an ajaxical call which you fire in onAllComplete. You can use jQuery.ajax or consorts for this.

E.g.

        'onAllComplete': function(event, queueID, fileObj, response, data) {
             $.post('somescript.aspx', paramsWhichSignalsServerToSendMail);
        },
BalusC
The handler is executed once for every file. I need to send e-mail when all files (1 or more) are complete.
David
@David: I see :)
BalusC
you get the upvote for the code since I think it came in after Pekka's suggestion for the postback.
David
Actually, after your comment and maybe also because pekka mentioned `onComplete` instead of `onAllComplete`.
BalusC