日期:2014-05-16  浏览次数:20378 次

asp.net中的ashx页无法读取上传的文件
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,请问是什么原因?
------解决方案--------------------
context.Request.Files[0]
------解决方案--------------------
你这种方法是没有办法读取到到file的,可以用jquery form来实现
------解决方案--------------------
必须是post提交,并且form  enctype="multipart/form-data"
------解决方案--------------------
jQuery上传文件

<htm