2

I'm trying to use Uploadify in a ASP.NET webforms project. The problem is that my script is not calling the generic handler. Here is the script.

<input id="fileInput" name="fileInput" type="file" />
<script type="text/javascript">
    $(document).ready(function() {
    $('#fileInput').uploadify({
        'uploader': '/Ferramenta/Comum/Uploadify/uploadify.swf',
        'script': 'UploadTest.ashx',
        'cancelImg': '/Ferramenta/Comum/Uploadify/cancel.png',
        'folder': "/Ferramenta/Geral/",
        'auto': true,
        'onError': function(event, queueID, fileObj, errorObj) {
            alert('error');
        },
        'onComplete': function(event, queueID, fileObj, response, data) {
            alert('complete');
        },
        'buttonText' : 'Buscar Arquivos'
    });
});
</script>

This is the code of the generic handler (just to test)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;

namespace Tree.Ferramenta.Geral
{
    public class UploadTest : IHttpHandler
    {
        public void ProcessRequest(HttpContext context) 
        {
            context.Response.Write("1");
        }

        public bool IsReusable
        {
             get
             {
                return false;
             }
        }
    }
}

Any ideas ? Thanks !

João Guilherme
  • 1,371
  • 1
  • 15
  • 27
  • 1
    http://trycatchfail.com/blog/post/2009/05/13/using-flash-with-aspnet-mvc-and-authentication.aspx –  Sep 28 '10 at 06:36

5 Answers5

1

Have you tried the code on IE and Firefox? If you only get this problem with Firefox, try creating a Global.asax with the following code (I just have the code in VB.NET):

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    '' Fix for the Flash Player Cookie bug in Non-IE browsers.
    '' Since Flash Player always sends the IE cookies even in FireFox
    '' we have to bypass the cookies by sending the values as part of the POST or GET
    '' and overwrite the cookies with the passed in values.

    '' The theory is that at this point (BeginRequest) the cookies have not been ready by
    '' the Session and Authentication logic and if we update the cookies here we'll get our
    '' Session and Authentication restored correctly
    Try
        Dim session_param_name As String = "ASPSESSID"
        Dim session_cookie_name As String = "ASP.NET_SESSIONID"

        If HttpContext.Current.Request.Form(session_param_name) IsNot Nothing Then
            UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form(session_param_name))
        ElseIf HttpContext.Current.Request.QueryString(session_param_name) IsNot Nothing Then
            UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString(session_param_name))
        End If
    Catch ex As Exception

    End Try

    Try
        Dim auth_param_name As String = "AUTHID"
        Dim auth_cookie_name As String = FormsAuthentication.FormsCookieName

        If HttpContext.Current.Request.Form(auth_param_name) IsNot Nothing Then
            UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form(auth_param_name))
        ElseIf HttpContext.Current.Request.QueryString(auth_param_name) IsNot Nothing Then
            UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString(auth_param_name))
        End If
    Catch ex As Exception

    End Try
End Sub

Private Sub UpdateCookie(ByVal cookie_name As String, ByVal cookie_value As String)
    Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies.Get(cookie_name)

    If cookie Is Nothing Then
        cookie = New HttpCookie(cookie_name)
    End If

    cookie.Value = cookie_value
    HttpContext.Current.Request.Cookies.Set(cookie)
End Sub

And then call uploadify like this:

<input id="fileInput" name="fileInput" type="file" />
<script type="text/javascript">
    $(document).ready(function() {
    var AUTHID = '<%= IIf(Request.Cookies(FormsAuthentication.FormsCookieName) Is Nothing, String.Empty, Request.Cookies(FormsAuthentication.FormsCookieName).Value) %>';
    var ASPSESSID = '<%= Session.SessionID %>';
    $('#fileInput').uploadify({
        'uploader': '/Ferramenta/Comum/Uploadify/uploadify.swf',
        'script': 'UploadTest.ashx',
        'scriptData': { 'ASPSESSID': ASPSESSID, 'AUTHID': AUTHID },
        'cancelImg': '/Ferramenta/Comum/Uploadify/cancel.png',
        'folder': "/Ferramenta/Geral/",
        'auto': true,
        'onError': function(event, queueID, fileObj, errorObj) {
            alert('error');
        },
        'onComplete': function(event, queueID, fileObj, response, data) {
            alert('complete');
        },
        'buttonText' : 'Buscar Arquivos'
    });
});
</script>
pauline
  • 11
  • 2
1

You can check if the namespace for the handler class (UploadTest.ashx) is the same in the markup and code behind – for me it was the thing causing the same problem.

kaarel
  • 617
  • 4
  • 13
1

Put a breakpoint in ProcessRequest.. does it fire?

If your website content is not public, add to web.config authorization access to the Handler, in order to avoid HTTP Error

<location path="Upload.ashx">
   <system.web>
     <authorization>
       <allow users="*"/>
     </authorization>
   </system.web>
 </location>
Emanuele Greco
  • 12,551
  • 7
  • 51
  • 70
0

Browse directly to your .ashx file, do you get any errors?

Download firebug, and check to see if you are receiving any javascript errors as well.

You might also want to add:

context.Response.ContentType = "text/plain";
context.Response.StatusCode = 200;
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
  • Jack, no errors when I browse directly the ashx file. No errors on firebug. I added the code that you sent, but still not working. I downloaded this ( http://gyoshev.net/temp/stackoverflow/Uploadify.zip ) code from Alexander Gyoshev that he posted on this question http://stackoverflow.com/questions/1327972/using-uploadify-with-sharepoint-and-net. It works. But I still didn't find the problem on my project. – João Guilherme Mar 04 '10 at 16:09
  • Perhaps, your path to: 'script': 'UploadTest.ashx', is incorrect? – Jack Marchetti Mar 04 '10 at 16:13
  • Jack, I guess not because the files are on the same folder. – João Guilherme Mar 04 '10 at 16:22
  • If you set a breakpoint in your handler, does it ever hit the breakpoint? – Jack Marchetti Mar 04 '10 at 16:39
  • No it doesn't. That's the problem. – João Guilherme Mar 04 '10 at 18:12
0

Is your site on SSL? I had the same problem and it turned out it was because I had an invalid certificate. Something to do with Flash burying an error if you are uploading a file with a bad cert.