asp.net |通过ajax发送的值在处理程序中作为null接收

时间:2014-07-25 11:05:09

标签: c# asp.net

我正在尝试将'file'附件发送到处理程序(并通过电子邮件发送)

这是我用来将它发送给处理程序(JS)的代码

当我调试它时,我看到了正确的值,包括文件(在java脚本中调试)。

function sendCv_click() {    
var settings = {
    'data': getData("sendCv"),
    'url': "Handlers/SendCV.ashx",
    'contentType': 'false',
    'processData': 'false'
};
sendCV(settings);
};

function getData(){
     data = {
            'fullName': $('#txt_sendCv_fullName').val(),
            'cvFile':$('#fu_sendCv_upload')[0].files[0],
            'email': $('#txt_sendCv_email').val(),
            'checkBox': $('#sendCv_chkBox:checked').length
}

function sendCV(settings) {
        $.ajax({
            type: "POST",
            contentType: settings.contentType,
            processData: settings.processData, 
            data: settings.data,
            url: settings.url,
            dataType: "json",
           success: function(data) {
                ...
            },
            error: function(data, xhr) {
               ...
           });
        }).always(function() {
            ...
        });       
    }
}

如何在处理程序页面中获取它们?

像那样我把它们当作Null,为什么?

public void ProcessRequest(HttpContext context)
{      
    string fullName = context.Request.Form.Get("fullName"); //It's Null
    string email = context.Request.Form.Get("email");//It's Null
    string chkBox_ad = context.Request.Form.Get("checkBox");  //It's Null
    /////how can i get the file here ??

    bool mailSent = Mail.SendCV(fullName, email, chkBox_ad);
    context.Response.ContentType = "text/plain";

    if (mailSent)
    {
        context.Response.Write("true");
    }
    else
    {
        context.Response.Write("false");
    }
}

2 个答案:

答案 0 :(得分:0)

这可能有用......

var postedFile = context.Request.Files[0];

答案 1 :(得分:0)

var parser = new MultipartParser(context.Request.InputStream);
if (parser.Success) {
  byte[] content = parser.FileContents;
  string filename = parser.Filename;
}

MultipartParser是开源课程 - 请参阅http://multipartparser.codeplex.com/SourceControl/latest#MultipartParser.cs