avinash ss
avinash ss

Reputation: 77

HttpPostedFileBase returns null every time

my client side javascript code is

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) {

            }
        });

    }

and my code behind function in controller is

    [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
                }
             }

I'm getting a null in fileval.

my file select is

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

I'm getting all other values I have passed except files.

Upvotes: 1

Views: 56

Answers (1)

Archana Parmar
Archana Parmar

Reputation: 578

@avinash I am attaching my code here. I have tested it and it is working fine. I hope this helps :-)

You can insert breakpoint in controller to check value for other parameters.

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!");
        }

    });

}

CONTROLLER CODE

 [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);
    }

Upvotes: 1

Related Questions