1

我正在尝试使用带有 Backload 的 jQuery File Upload 作为 ASP.NET 网站中的处理程序,但无法使其正常工作。

这是 Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE HTML>
<html>
<head runat="server">
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script src="js/uploader.js"></script>
</head>
<body>
<input id="fileupload" type="file">
</body> 
</html>

以及启用文件上传插件的js:

$(document).ready(function () {
    var handlerUrl = "/Backload/FileHandler";

    $('#fileupload').fileupload({
        url: handlerUrl
    });
});

我已经使用 NuGet 安装了 Backload,并将 jQuery File Upload 加载到我的项目中。所有引用都加载正常(控制台中没有错误)。当我尝试上传文件时出现错误: Failed to load resource: the server responded with a status of 404 (Not Found),并且指出的资源是http://localhost:61076/Backload/FileHandler.

我在这里想念什么?

注意:我没有编写任何代码。这些都是来自相关来源的复制\粘贴示例,因为我试图在实际构建自己的网站之前让一个基本的东西正常工作。

4

2 回答 2

2

刚刚看到您有一个 ASP.NET WebForms 项目。Backload 默认使用 MVC。有两种方法可以让 Backload 在经典的 WebForms 项目中运行:

  • 将 MVC NuGet 包添加到项目中,并在 RegisterRoutes() 中添加路由
  • 或者,添加并注册 (Web.Config) 一个 HttpHandler。这里可以删除“~/Backload/Controller”中的控制器,避免MVC依赖。

最简单的方法是添加 MVC NuGet 包并在“~/App_Start/RouteConfig.cs”中配置路由:

    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }


第二种解决方案(添加 HttpHandler)如下所示: https ://github.com/blackcity/Backload/tree/master/Examples/Backload.Standard.2.0.Full/Backload.Demo/Other/Handler

例子:

public class FileHandler : HttpTaskAsyncHandler
{
    /// <summary>
    /// File handler demo for classic Asp.Net or HTML. 
    /// To access it in an Javascript ajax request use: <code>var url = "/Handler/FileHandler.ashx";</code>.
    /// </summary>
    /// <remarks>
    /// NOTE. Edit the web.config file to allow the DELETE method in the system.webServer.handlers section
    /// </remarks>
    public override async Task ProcessRequestAsync(HttpContext context)
    {
        try
        {
            // Wrap the request into a HttpRequestBase type
            HttpRequestBase request = new HttpRequestWrapper(context.Request);


            // Create and initialize the handler
            IFileHandler handler = Backload.FileHandler.Create();
            handler.Init(request);


            // Call the execution pipeline and get the result
            IBackloadResult result = await handler.Execute();


            // Write result to the response and flush
            ResultCreator.Write(context.Response, result);
            context.Response.Flush();

        }
        catch
        {
            context.Response.StatusCode = 500;
        }

    }
}
于 2015-10-13T16:10:48.597 回答
0

我认为您在 FileHandler 操作名称 http://localhost:61076/Backload/FileHandler中错过了“r”

于 2015-10-12T09:53:29.793 回答