asp.net中的ashx页无法读取上传的文件

threeperson 2012-04-26 05:02:29
a.ascx页面通过Jquery的Ajax异步上传文件,转到b.ashx页处理。所有参数都能a.ashx页获取,但就是无法读取到文件。

a.ascx的相关代码如下:
function PostImage() {
$.post("../../../AjaxResponse/Handler.ashx?time=" + Math.random(), {
imgPath: $("#imgFileUpload").val(),
action: "checkSize",
filepath: "temp",
maxSize: "5000"
},
function (data) {

$("#imgPreview").attr("src", data.toString()); //显示缩略图

});

}


b.ashx页面的相关代码如下:
public class TypeEditCoverHandler : IHttpHandler
{

//获取栏目类别的封面图片
public void ProcessRequest(HttpContext context)
{
//context.Request.ContentType = "multipart/form-data";
context.Response.ContentType = "text/plain";

//获取页面传过来的状态
string strState = context.Request["action"];
//获取文件大小限制
int maxSize = Moore.Help.DataConverter.StrToInt(context.Request["maxSize"]);
//获取文件路径
string strImgPath = context.Request["imgPath"]; //能正确读到:D:\图片上传测试\04.jpg
HttpPostedFile imgPostFile = context.Request.Files[strImgPath]; //一直为null
//临时文件名称
string strTempImageName = String.Empty;
…… 后面的略


HttpPostedFile imgPostFile这句得到的文件一直为null,请问是什么原因?
...全文
909 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
白牛牛牛 2014-03-09
  • 打赏
  • 举报
回复
引用 12 楼 threeperson 的回复:
问题已解决,原因是我把图片上传的部分做为一个用户控件ascx封装起来了,拖到页面时,造成<form>标签嵌套。 因此报一些未知的错误。
我也遇到这种情况,请问你怎么修改的呢?
战斗模式 2013-06-14
  • 打赏
  • 举报
回复
引用 7 楼 net_lover 的回复:
jQuery上传文件 <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <script src="jquery-1.7.1.min.js"></script> <script src="jquery.form.js"></script> <script type="text/javascript"> function upload() { $("#form1").ajaxSubmit({ success: function (str) { alert(str); }, error: function (error) { alert(error); }, url: 'handler1.ashx', /*设置post提交到的页面*/ type: "post", /*设置表单以post方法提交*/ dataType: "text" /*设置返回值类型为文本*/ }); } </script> </head> <body> <form id="form1" runat="server" enctype="multipart/form-data"> <input type="file" id="file" name="file" /> <asp:Button ID="Button1" runat="server" Text="上传" OnClientClick="upload();return false;" /> </form> </body> handler1.ashx <%@ WebHandler Language="C#" Class="handler1" %> using System; using System.Web; public class handler1 : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; HttpPostedFile file = context.Request.Files[0]; String fileName = System.IO.Path.GetFileName(file.FileName); file.SaveAs(context.Server.MapPath("~/") + fileName); context.Response.Write("OK"); } public bool IsReusable { get { return false; } } } jquery.form.js下载地址 http://malsup.github.com/jquery.form.js
这段代码很好,我们已经用到了,谢谢!
threeperson 2012-04-27
  • 打赏
  • 举报
回复
问题已解决,原因是我把图片上传的部分做为一个用户控件ascx封装起来了,拖到页面时,造成<form>标签嵌套。 因此报一些未知的错误。
threeperson 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

类似的代码网上很多
http://www.itivy.com/ivy/archive/2011/2/11/634330390386716259.html
[/Quote]

在线没,能否远程帮我调一下。实在找不出什么原因了。
孟子E章 2012-04-26
  • 打赏
  • 举报
回复
类似的代码网上很多
http://www.itivy.com/ivy/archive/2011/2/11/634330390386716259.html
threeperson 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

jQuery上传文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="jquery-1.7.1.min.js"></script>
<script src="jquery.form.js"></script>
<script type="t……
[/Quote]
按这种复制过去,也会报错。
        $("#frmImageUpload").ajaxSubmit({
success: function (str) {
alert(str);
},
error: function (error) { alert(error); },
url: '../../../AjaxResponse/TypeEditCoverHandler.ashx', /*设置post提交到的页面*/
type: "post", /*设置表单以post方法提交*/
dataType: "text" /*设置返回值类型为文本*/
});

提示不支持该属性或方法
孟子E章 2012-04-26
  • 打赏
  • 举报
回复
jQuery上传文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="jquery-1.7.1.min.js"></script>
<script src="jquery.form.js"></script>
<script type="text/javascript">
function upload() {
$("#form1").ajaxSubmit({
success: function (str) {
alert(str);
},
error: function (error) { alert(error); },
url: 'handler1.ashx', /*设置post提交到的页面*/
type: "post", /*设置表单以post方法提交*/
dataType: "text" /*设置返回值类型为文本*/
});
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="file" id="file" name="file" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClientClick="upload();return false;" />
</form>
</body>

handler1.ashx

<%@ WebHandler Language="C#" Class="handler1" %>

using System;
using System.Web;

public class handler1 : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
HttpPostedFile file = context.Request.Files[0];
String fileName = System.IO.Path.GetFileName(file.FileName);
file.SaveAs(context.Server.MapPath("~/") + fileName);
context.Response.Write("OK");
}

public bool IsReusable {
get {
return false;
}
}
}

jquery.form.js下载地址
http://malsup.github.com/jquery.form.js
孟子E章 2012-04-26
  • 打赏
  • 举报
回复
必须是post提交,并且form enctype="multipart/form-data"
threeperson 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

你这种方法是没有办法读取到到file的,可以用jquery form来实现

[/Quote]

这种方法没办法实现?
骑猪看海 2012-04-26
  • 打赏
  • 举报
回复
你这种方法是没有办法读取到到file的,可以用jquery form来实现
threeperson 2012-04-26
  • 打赏
  • 举报
回复
难道是跨页面的问题?
threeperson 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

context.Request.Files[0]
[/Quote]
这样写之后,提示:索引超出范围。必须为非负值并小于集合大小。
参数名: index
孟子E章 2012-04-26
  • 打赏
  • 举报
回复
context.Request.Files[0]

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧