晨风

-------------------- 业精于勤,荒于嬉;行成于思,毁于随

导航

context.Request.Files为NULL问题

Posted on 2016-03-30 09:29  shenyixin  阅读(1226)  评论(0编辑  收藏  举报

在实现图片上传功能的时候出现在ashx等处理页面出现context.Request.Files为NULL异常,有几点需要注意:

1、在客户端可以将form用submit提交,如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="JS/jquery-1.3.2.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        function UpLoad() {
            $("#form1").submit();            
        }         
    </script>
</head>
<body>
    <form id="form1" method="post" enctype="multipart/form-data" action="ImageHandler.ashx">
    <div>
        <input id="File1" type="file" runat="server"/>
        <input id="Button1" type="button" value="上传" onclick="UpLoad()" /></div> 
        <div id="imgList"></div>      
    </form>
</body>
</html>

2、form表单需要加上属性method="post" enctype="multipart/form-data"

3、文件控件需要加上runat="server"属性

转自:http://www.cnblogs.com/tianguook/archive/2011/06/18/2084139.html