【问题标题】:Uploadify not working with ASP.NET WebFormsUploadify 不适用于 ASP.NET WebForms
【发布时间】:2010-03-04 15:28:39
【问题描述】:

我正在尝试在 ASP.NET 网络表单项目中使用 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;
             }
        }
    }
}

有什么想法吗?谢谢!

【问题讨论】:

标签: asp.net jquery uploadify webforms


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以检查处理程序类 (UploadTest.ashx) 的命名空间在标记和后面的代码中是否相同 - 对我来说,这是导致相同问题的原因。

    【讨论】:

      【解决方案3】:

      ProcessRequest.. 中设置断点。它会触发吗?

      如果你的网站内容不公开,添加web.config对Handler的授权访问,以避免HTTP Error

      <location path="Upload.ashx">
         <system.web>
           <authorization>
             <allow users="*"/>
           </authorization>
         </system.web>
       </location>
      

      【讨论】:

        【解决方案4】:

        直接浏览到您的 .ashx 文件,是否有任何错误?

        下载 firebug,并检查您是否也收到任何 javascript 错误。

        您可能还想添加:

        context.Response.ContentType = "text/plain";
        context.Response.StatusCode = 200;
        

        【讨论】:

        • 杰克,当我直接浏览 ashx 文件时没有错误。萤火虫没有错误。我添加了您发送的代码,但仍然无法正常工作。我从 Alexander Gyoshev 下载了这个(gyoshev.net/temp/stackoverflow/Uploadify.zip)代码,他在这个问题上发布了stackoverflow.com/questions/1327972/…。有用。但是我仍然没有发现我的项目的问题。
        • 也许你的路径:'script': 'UploadTest.ashx',不正确?
        • 杰克,我猜不是因为文件在同一个文件夹中。
        • 如果您在处理程序中设置断点,它是否会遇到断点?
        • 不,它没有。这就是问题所在。
        【解决方案5】:

        您的网站是否使用 SSL? 我遇到了同样的问题,原来是因为我的证书无效。如果您上传证书错误的文件,则与 Flash 隐藏错误有关。

        【讨论】:

          猜你喜欢
          • 2011-12-14
          • 2016-07-27
          • 1970-01-01
          • 1970-01-01
          • 2016-07-09
          • 1970-01-01
          • 2011-08-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多