C# 提交到ashx时Jquery文件上载错误
我正在尝试使用该插件将文件异步上载到C3 http处理程序。我已经完成了项目的安装步骤。它在Firefox中似乎工作正常,但在IE中抛出了一个javascript错误(“抛出异常且未捕获”。第95行,Char 25,File:test.html),即使该文件已成功上载。我认为我的问题与我的ashx的反应有关。有人能看出我做错了什么吗 以下是我的页面(test.html)的html正文:C# 提交到ashx时Jquery文件上载错误,c#,jquery,file-upload,httphandler,C#,Jquery,File Upload,Httphandler,我正在尝试使用该插件将文件异步上载到C3 http处理程序。我已经完成了项目的安装步骤。它在Firefox中似乎工作正常,但在IE中抛出了一个javascript错误(“抛出异常且未捕获”。第95行,Char 25,File:test.html),即使该文件已成功上载。我认为我的问题与我的ashx的反应有关。有人能看出我做错了什么吗 以下是我的页面(test.html)的html正文: 上传 上传文件 /*全球美元*/ $(函数(){ $(“#文件上传”).fileUploadUI({ 上载表
上传
上传文件
/*全球美元*/
$(函数(){
$(“#文件上传”).fileUploadUI({
上载表:$(“#文件”),
下载表:$(“#文件”),
buildUploadRow:函数(文件、索引){
返回$(''+文件[索引]。名称+''+
'' +
'' +
'' +
“取消”+
'');
},
buildDownloadRow:函数(文件){
返回$(''+文件名+'');
}
});
});
以下是我的ashx中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace Testing
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
public class TestUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpPostedFile fileupload = context.Request.Files[0];
string strFileName = Path.GetFileName(fileupload.FileName);
string strExtension = Path.GetExtension(fileupload.FileName).ToLower();
string strSaveLocation = context.Server.MapPath("Upload") + "\\" + strFileName;
fileupload.SaveAs(strSaveLocation);
context.Response.ContentType = "text/plain";
context.Response.Write("{\"name\":\"" + fileupload.FileName + "\",\"type\":\"" + fileupload.ContentType + "\",\"size\":\"" + fileupload.ContentLength + "\"}");
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.IO;
命名空间测试
{
///
///$codebehindclassname的摘要说明$
///
公共类TestUpload:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
HttpPostedFile fileupload=context.Request.Files[0];
字符串strFileName=Path.GetFileName(fileupload.FileName);
string strExtension=Path.GetExtension(fileupload.FileName.ToLower();
字符串strSaveLocation=context.Server.MapPath(“上载”)+“\\”+strFileName;
fileupload.SaveAs(strSaveLocation);
context.Response.ContentType=“text/plain”;
context.Response.Write(“{\”name\“:\”+fileupload.FileName+“\”,\”type\“:\”+fileupload.ContentType+“\”,\”size\“:\”+fileupload.ContentLength+“\”);
}
公共布尔可重用
{
得到
{
返回true;
}
}
}
}
尝试更改:
context.Response.Write("{\"name\":\"" + fileupload.FileName + "\",\"type\":\"" + fileupload.ContentType + "\",\"size\":\"" + fileupload.ContentLength + "\"}");
为此:
context.Response.Write("{\"name\":\"" + fileupload.FileName + "\", type:\"" + fileupload.ContentType + "\",size:\"" + fileupload.ContentLength + "\"}");
你传递回来的对象只是格式不正确。这应该可以解决问题。我能让它正常工作 首先,我尝试对名称、类型和大小响应使用硬编码值,并发现它有效。以下是我使用的:
context.Response.Write("{\"name\":\"test\",\"type\":\"test\",\"size\":\"12345\"}");
然后我注意到fileupload.FileName给出了文件的完整路径,并假设这就是问题所在。当我在response.write行中将fileupload.FileName更改为strFileName时,它在Firefox和IE中都能成功工作。下面是一行:
context.Response.Write("{\"name\":\"" + strFileName + "\",\"type\":\"" + fileupload.ContentType + "\",\"size\":\"" + fileupload.ContentLength + "\"}");
哎哟……JS错误是“异常已抛出且未捕获”。第95行,字符25。我会更新我的帖子。谢谢你的回复。我尝试了那个更改,但它无法修复错误。它还破坏了Firefox。这是一个非常无用的IE javascript错误——我想我可以追溯到jquery.min中的一行。与此同时,我对ashx的反应进行了一些研究,找到了解决方案。我会把它作为答案贴出来。谢谢你的帮助!
context.Response.Write("{\"name\":\"" + strFileName + "\",\"type\":\"" + fileupload.ContentType + "\",\"size\":\"" + fileupload.ContentLength + "\"}");