我试图将带有Backload的jQuery文件上传用作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
。
我在这里缺少什么?
注意:我没有写任何此代码。这些都是来自相关来源的复制\粘贴示例,因为我试图在实际构建自己的网站之前获得基本的工作。
答案 0 :(得分:1)
刚刚看到你有一个ASP.NET WebForms项目。 Backload默认使用MVC。有两种方法可以在经典的WebForms项目中运行Backload:
最简单的方法是添加MVC NuGet包并在&#34;〜/ App_Start / RouteConfig.cs&#34;中配置路由:
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;
}
}
}
答案 1 :(得分:0)
我认为您在FileHandler操作名称中错过了“r” http://localhost:61076/Backload/FileHandler