HttpPostedFileBase每次都返回null

时间:2017-09-18 12:59:47

标签: javascript c# jquery asp.net ajax

我的客户端javascript代码是

function SendMail() {
        debugger;
        var decpr = tinyMCE.get('taskdescription').getContent();       
        var To = $('#EmailTo').val();
        var cc = $('#EmailCC').val();
        var sub = $('#Subject').val();
        var Desc = $(decpr).text();
        var UserName = $('#resname').val();
        $("#frm1").attr('action', '/Task/SendWeeklyReport.aspx?To=' + To + '&cc=' + cc + '&subject=' + sub + '&desc=' + Desc + '&Username=' + UserName);
        $("#frm1").attr('enctype', 'multipart/form-data');
        $("#frm1").attr('method', 'post');           
        $('#frm1').ajaxSubmit({
            datatype: 'text',
            success: function (data, text) {
                $("#frm1").removeAttr('action');
                $("#frm1").removeAttr('enctype');
                $("#frm1").removeAttr('method');
            },
            error: function (request, status, error) {

            }
        });

    }

我在控制器中的函数后面的代码是

    [HttpPost]
            public virtual ActionResult SendWeeklyReport(HttpPostedFileBase[] fileval, string To, string cc, string subject, string desc, string Username)
            {

                foreach (var file in fileval)
                {
                    // some file manipulation
                }
             }

我在fileval中得到一个null。

我的文件选择是

<input type="file" name="fileval" multiple id="fileselect" style="margin-left: 15px; position: relative; width: 96px" /> 

我得到了除文件以外的所有其他值。

1 个答案:

答案 0 :(得分:1)

@avinash我在这里附上我的代码。我测试了它,它工作正常。我希望这会有所帮助: - )

您可以在控制器中插入断点以检查其他参数的值。

<强> HTML

<div>
<label>To Email</label>
<input type="text" id="To" class="form-control" />
<br />
<label>Cc Email</label>
<input type="text" id="CC" class="form-control" />
<br />
<label>Subject</label>
<input type="text" id="Subject" class="form-control" />
<br />
<label>Desc</label>
<textarea id="Desc" class="form-control"></textarea>
<br />
<label>Choose File</label>
<input type="file" style="width: 86% !important;" class="form-control" id="fileUpload" />
<br />
<input type="button" onclick="SaveFile()" class="btn btn-primary" value="Save" />

<强> JS

function SaveFile() {
    var ToEmail = $("#To").val();
    var CcEmail = $("#CC").val();
    var Subject = $("#Subject").val();
    var Desc = $("#Desc").val();

    var fileUploaded = $("#fileUpload").get(0);
    var files = fileUploaded.files;
    var fileData = new FormData();
    for (var i = 0; i < files.length; i++) {
        fileData.append(files[i].name, files[i]);
    }

    fileData.append("ToEmail", ToEmail);
    fileData.append("CcEmail", CcEmail);
    fileData.append("Subject", Subject);
    fileData.append("Desc", Desc);

    $.ajax({
        url: "@Url.Action("UploadAndSaveFile", "Employee")",
        type: "POST",
        dataType: "json",
        contentType: false,
        processData: false,
        data: fileData,
        success: function (data) {
            $("#fileUpload").val('');
        },
        error: function (response) {
            alert("File uploading failed due to an error. Please Try again!");
        }

    });

}

控制器代码

 [HttpPost]
    public JsonResult UploadAndSaveFile(string ToEmail, string CcEmail, string Subject, string Desc)
    {
        if (Request.Files.Count > 0)
        {
            //  Get all files from Request object  
            HttpFileCollectionBase files = Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFileBase file = files[i];
                byte[] fileContent = new byte[file.ContentLength];
                file.InputStream.Read(fileContent, 0, Convert.ToInt32(file.ContentLength));
                string Filename = file.FileName;
                string extension = Path.GetExtension(file.FileName);
            }
        }
        return Json("", JsonRequestBehavior.AllowGet);
    }