Javascript 如何将ASP.NET处理程序的url传递给jquery fileupload?
我使用JQuery文件上传,和ASP.NET4.0Web应用程序项目 但我不知道如何传递我的ASP.NET C#处理程序url 我想知道如何正确编写AjaxFileHandler的url 我尝试将ASP.NET处理程序设置为“AjaxFileHandler.ashx”和“url:AjaxFileHandler.ashx”,但出现了一个错误 POST 500(内部服务器错误) -Post.aspx-Javascript 如何将ASP.NET处理程序的url传递给jquery fileupload?,javascript,jquery,asp.net,jquery-file-upload,Javascript,Jquery,Asp.net,Jquery File Upload,我使用JQuery文件上传,和ASP.NET4.0Web应用程序项目 但我不知道如何传递我的ASP.NET C#处理程序url 我想知道如何正确编写AjaxFileHandler的url 我尝试将ASP.NET处理程序设置为“AjaxFileHandler.ashx”和“url:AjaxFileHandler.ashx”,但出现了一个错误 POST 500(内部服务器错误) -Post.aspx- <script type="text/javascript"> $(func
<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>
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
}
$(函数(){
$('#fileupload')。fileupload({
数据类型:“json”,
url:'AjaxFileHandler.ashx',
limitConcurrentUploads:1,
顺序上传:对,
maxChunkSize:100000,
添加:功能(e、数据){
$('#filelistholder').removeClass('hide');
data.context=$('').text(data.files[0].name).appendTo('#filelistholder');
$(' \
\
\
).appendTo(data.context);
$('#btnUploadAll')。单击(函数(){
data.submit();
});
},
完成:功能(e,数据){
data.context.text(data.files[0].name+'(전송완료)');
$(' \
\
\
).appendTo(data.context);
},
progressall:功能(e、数据){
var progress=parseInt(data.loaded/data.total*100,10);
$('#overallbar').css('width',progress+'%');
},
进度:功能(e、数据){
var progress=parseInt(data.loaded/data.total*100,10);
data.context.find('.progress bar').css('width',progress+'%');
}
});
});
函数updateContent(){
oEditors.getById[“postContent”].exec(“更新内容字段,[]);
}
-AjaxFileHandler.ashx-
<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>
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
}
使用系统;
使用System.Web;
使用System.IO;
公共类AjaxFileHandler:IHttpHandler
{
#区域IHttpHandler成员
公共布尔可重用{get{return true;}}
公共void ProcessRequest(HttpContext上下文)
{
//在这里编写处理程序实现。
如果(context.Request.Files.Count>0)
{
字符串路径=context.Server.MapPath(“/UploadedFiles/”);
如果(!Directory.Exists(path))
{
CreateDirectory(路径);
}
var file=context.Request.Files[0];
字符串文件名=Path.Combine(Path,file.fileName);
file.SaveAs(文件名);
context.Response.ContentType=“text/plain”;
context.Response.Write(“console.log(“+fileName+”);”;
var serializer=new System.Web.Script.Serialization.JavaScriptSerializer();
var result=new{name=file.FileName};
context.Response.Write(serializer.Serialize(result));
}
}
#端区
}
您可以在项目中创建AjaxFileHandler.ashx处理程序,并调用url,如url:“AjaxFileHandler.ashx”
您收到错误是因为您的项目中不存在这样的url()您需要在web.config文件中添加url(),例如:
<configuration>
<system.webServer>
<handlers>
<add name="AjaxFileHandler" verb="*"
path="AjaxFileHandler.ashx"
type="UCTS.Board.AjaxFileHandler" />
</handlers>
</system.webServer>
</configuration>
注意:path属性的值定义了用于调用处理程序的URL。在上面的示例中,它将是
URL:'AjaxFileHandler.ashx'
我发现.ashx文件顶部缺少关于处理程序的初始化
在AjaxFileHandler.ashx中添加了下面的句子,并正在进行处理
使用制度;
使用System.IO;
使用System.Web;
公共类AjaxFileHandler:IHttpHandler
{
#区域IHttpHandler成员
公共布尔可重用
{
//如果无法将托管处理程序重新用于其他请求,则返回false。
//通常,如果每个请求都保留了一些状态信息,那么这将是错误的。
获取{return true;}
}
公共void ProcessRequest(HttpContext上下文)
{
//在这里编写处理程序实现。
如果(context.Request.Files.Count>0)
{
字符串路径=context.Server.MapPath(“/UploadedFiles/”);
如果(!Directory.Exists(path))
{
CreateDirectory(路径);
}
var file=context.Request.Files[0];
字符串文件名=Path.Combine(Path,file.fileName);
file.SaveAs(文件名);
context.Response.ContentType=“text/plain”;
context.Response.Write(“console.log(“+fileName+”);”;
var serializer=new System.Web.Script.Serialization.JavaScriptSerializer();
var result=new{name=file.FileName};
context.Response.Write(serializer.Serialize(result));
}
}
#端区
}
printscreen可读性不强。请在问题中键入代码。@PavelV。)哎呀..我编辑了我的帖子。同时出现了一个错误posthttp://localhost:5468/AjaxFileHandler.ashx 500(内部服务器错误)
您可以调试ashx文件中的代码吗?或者您可以提供有关错误的更多详细信息吗(错误代码500可以是与服务器相关的任何内容)。如果您使用IE,您可以通过此操作获得更多错误详细信息-转到“Internet选项”,然后选择“高级”,并取消选中“显示友好错误消息”选项我使用chrome。这是我制作的测试代码zip文件。我尝试了你的建议,但没有成功。我可以看到一个错误POSThttp://localhost:5468/AjaxFileHandler.ashx 500(内部服务器错误)
,我使用chrome。这是我的测试代码zip文件@NathanielJobs:在ProcessRequest()中放置断点
方法,并启动调试器。然后从web浏览器调用。visual studio是否在断点处停止?实际上ProcessRequest()
方法不会