我使用JQuery文件上传和ASP.NET 4.0 Web应用程序项目。
但我不知道如何传递我的ASP.NET C#处理程序URL ...
我想知道如何正确编写AjaxFileHandler的URL?
我尝试过制作ASP.NET Handler" AjaxFileHandler.ashx"和" url:AjaxFileHandler.ashx"但出现错误
POST http://localhost:5468/AjaxFileHandler.ashx 500(内部服务器错误)
-Post.aspx -
<script type="text/javascript">
$(function () {
$('#fileupload').fileupload({
datatype: "json",
url: 'AjaxFileHandler.ashx',
limitConcurrentUploads: 1,
sequentialUpload: true,
maxChunkSize: 100000,
add: function (e, data) {
$('#filelistholder').removeClass('hide');
data.context = $('<div>').text(data.files[0].name).appendTo('#filelistholder');
$('</div> \
<div class="progress"> \
<div class="progress-bar" style="width: 0%;"></div> \
</div>').appendTo(data.context);
$('#btnUploadAll').click(function () {
data.submit();
});
},
done: function (e, data) {
data.context.text(data.files[0].name + ' (전송완료)');
$('</div> \
<div class="progress"> \
<div class="progress-bar" style="width: 100%"></div> \
</div>').appendTo(data.context);
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#overallbar').css('width', progress + '%');
},
progress: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
data.context.find('.progress-bar').css('width', progress + '%');
}
});
});
function updateContent() {
oEditors.getById["postContent"].exec("UPDATE_CONTENTS_FIELD", []);
}
</script>
-AjaxFileHandler.ashx -
using System;
using System.Web;
using System.IO;
public class AjaxFileHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable { get { return true; }}
public void ProcessRequest(HttpContext context)
{
//write your handler implementation here.
if (context.Request.Files.Count > 0)
{
string path = context.Server.MapPath("/UploadedFiles/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var file = context.Request.Files[0];
string fileName = Path.Combine(path, file.FileName);
file.SaveAs(fileName);
context.Response.ContentType = "text/plain";
context.Response.Write("<script>console.log('" + fileName + "');</script>");
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var result = new { name = file.FileName };
context.Response.Write(serializer.Serialize(result));
}
}
#endregion
}
答案 0 :(得分:1)
您可以在项目中创建AjaxFileHandler.ashx处理程序并调用类似url:"AjaxFileHandler.ashx"
您收到错误是因为您的项目中没有此类网址(http://localhost:5468/AjaxFileHandler)
答案 1 :(得分:1)
我发现缺少初始化 .ashx 文件顶部的处理程序
在AjaxFileHandler.ashx中添加了以下句子并正在处理它。
&lt;%@ WebHandler Language =“C#”Class =“AjaxFileHandler”%&gt;
<%@ WebHandler Language="C#" Class="AjaxFileHandler" %>
using System;
using System.IO;
using System.Web;
public class AjaxFileHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
// Return false in case your Managed Handler cannot be reused for another request.
// Usually this would be false in case you have some state information preserved per request.
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
//write your handler implementation here.
if (context.Request.Files.Count > 0)
{
string path = context.Server.MapPath("/UploadedFiles/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var file = context.Request.Files[0];
string fileName = Path.Combine(path, file.FileName);
file.SaveAs(fileName);
context.Response.ContentType = "text/plain";
context.Response.Write("<script>console.log('" + fileName + "');</script>");
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var result = new { name = file.FileName };
context.Response.Write(serializer.Serialize(result));
}
}
#endregion
}
答案 2 :(得分:0)
您需要在web.config文件中register your HttpHandler,例如:
<configuration>
<system.webServer>
<handlers>
<add name="AjaxFileHandler" verb="*"
path="AjaxFileHandler.ashx"
type="UCTS.Board.AjaxFileHandler" />
</handlers>
</system.webServer>
</configuration>
注意:path属性的值定义用于调用处理程序的URL。在上面的示例中,它将是url: 'AjaxFileHandler.ashx'
。