Uploadify 不适用于 ASP.NET WebForms

2024-04-24

我正在尝试在 ASP.NET Webforms 项目中使用 Uploadify。问题是我的脚本没有调用通用处理程序。这是脚本。

<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>

这是通用处理程序的代码(仅用于测试)

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;
             }
        }
    }
}

有任何想法吗 ?谢谢 !


您在 IE 和 Firefox 上尝试过该代码吗?如果您只在 Firefox 中遇到此问题,请尝试使用以下代码创建 Global.asax(我只有 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

然后像这样调用 uploadify:

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Uploadify 不适用于 ASP.NET WebForms 的相关文章

随机推荐