Search code examples
c#jqueryasp.netasp.net-mvcuploadify

File upload returns null


I am uploading a file in my ASP.NET MVC application using Uploadify.

Controller:

public ActionResult Upload(HttpPostedFileBase file)
        {           
            List<string> validIDs, invalidIDs;
            if (file.ContentLength > 0)
            { //do something
            }
        }

Uploadify code (in a .ascx file):

$(document).ready(function () {   
    $("#file_upload").uploadify({
        'uploader': '/Scripts/uploadify/uploadify.swf',
        'script': '/XYZ/Upload',
        'cancelImg': '/Scripts/uploadify/cancel.png',
        'fileExt': '*.jpg;*.gif;*.png;*.bmp;*.htm;*.html;*.zip',
        'fileDesc': '*.jpg;*.gif;*.png;*.bmp;*.htm;*.html;*.zip',
        'auto': true,
        'multi': false,
        'sizeLimit': 1048576,   //1 MB
        'buttonText': 'Upload Files'
}
    });
});

The 'file' in the controller action is always returning NULL. What am I missing?


Solution

  • Replace:

    public ActionResult Upload(HttpPostedFileBase file)
    

    with:

    public ActionResult Upload(HttpPostedFileBase fileData)
    

    Uploadify uses the fileData name by default. You could change that in the settings if you want like that: fileDataName: 'file'. Take a look at the following post as well.