首页 新闻 会员 周边

html上传文件到.ashx 显示response.files=0 求大神看下

0
悬赏园豆:10 [已解决问题] 解决于 2016-04-16 14:11

html:

        <form id="form1"  enctype="multipart/form-data" action="~/comm/inexcel.ashx?action=@ViewBag.action">
            <input type="file" name="file" />
            <input type="submit" value="put" />
        </form> 

inexcel.ashx

            string action = context.Request["action"];

            if  (context.Request.Files["file"].ContentLength > 0)
            {
                context.Response.Write(1);
            }
            else
            {
                context.Response.Write(0);
            }

结果

“/”应用程序中的服务器错误。

未将对象引用设置到对象的实例。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误: 


行 17:             string action = context.Request["action"];
行 18: 
行 19:             if  (context.Request.Files["file"].ContentLength > 0)
行 20:             {
行 21:                 context.Response.Write(1);

 

少时不知贵的主页 少时不知贵 | 初学一级 | 园豆:86
提问于:2016-04-15 09:53
< >
分享
最佳答案
0

html:

<!DOCTYPE html>

<html>

<head>

    <title></title>

</head>

<body>

    <form id="form1" method="post" enctype="multipart/form-data" action="/upload.ashx?action=aa">

        <input type="file" name="file"/>

        <input type="submit" value="put"/>

    </form>

</body>

</html>

ashx:

publicvoid ProcessRequest(HttpContext context)

{

    String action = context.Request["action"];

    var file = context.Request.Files["file"];

 

    context.Response.ContentType = "text/plain";

    context.Response.Write("action=" + action);

    context.Response.Write(Environment.NewLine);

    context.Response.Write("fileName=" + file.FileName);

}

输出:

 

 

收获园豆:6
蘇乞兒 | 菜鸟二级 |园豆:435 | 2016-04-15 17:40

谢谢老司机: context.Response.ContentType = "text/html";  主要是格式错了 谢谢

少时不知贵 | 园豆:86 (初学一级) | 2016-04-16 14:10
其他回答(2)
0

给form加上method="post"

收获园豆:2
写代码的小2B | 园豆:4371 (老鸟四级) | 2016-04-15 14:17

还是没有  request里面有 file,但是 request.files里面是空的

支持(0) 反对(0) 少时不知贵 | 园豆:86 (初学一级) | 2016-04-15 14:44
0

 

      <form id="form1"  method="post" runat="server" enctype="multipart/form-data" action="ServiceRobot.ashx">
            <input runat="server" type="file" name="file" />
            <input type="submit" value="put" />
        </form>

 如果你是用的Webform,就应该这么写

收获园豆:2
KMSFan | 园豆:108 (初学一级) | 2016-04-15 16:54

谢谢老司机: context.Response.ContentType = "text/html";  主要是格式错了 谢谢

支持(0) 反对(0) 少时不知贵 | 园豆:86 (初学一级) | 2016-04-16 14:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册