C Sharper
C Sharper

Reputation: 8646

HTML file uploader not giving files in Controller page

I have used html file uploader as:

<form action="@Url.Action("ApiUploadImage","PP")" method="POST" id="frmObservationScoring">
                        <input type="file" name="fUploadedFiles"><br>
                        <button class="buttoncss">Upload</button>
</form>

And ApiUploadImage method in PPController as:

public void ApiUploadImage([DataSourceRequest] DataSourceRequest request, IEnumerable<HttpPostedFileBase> files)
{
    var savedFilePaths = new List<string>();
    var applicationPath = System.Web.HttpContext.Current.Request.Url.Scheme + "://" + System.Web.HttpContext.Current.Request.Url.Authority + System.Web.HttpContext.Current.Request.ApplicationPath + "/Content/Images/Others/";
    if (files != null)
    {
        foreach (var file in files)
        {
            var fileName = Path.GetFileName(file.FileName);
            if (fileName != null)
            {
                fileName = DateTime.Now.ToString("yyyyMMddmm-") + fileName;
                var imagePath = Path.Combine(Server.MapPath("~/Content/Images/Others/"), fileName);
                file.SaveAs(imagePath);
                savedFilePaths.Add(applicationPath + fileName);
            }
        }
    }

    //return Json(new[] { savedFilePaths }.ToDataSourceResult(request));
}

Method is getting called but ,

IEnumerable<HttpPostedFileBase> files parameter is giving null value although i upload any file through html file uploader.

What can be the problem??

Please help me.

Upvotes: 0

Views: 40

Answers (2)

Saranga
Saranga

Reputation: 3228

In your HTML name should be files to match the controller.

<input type="file" name="files">

Thanks!

Upvotes: 1

tixie
tixie

Reputation: 99

In your html, you should add enctype attribute to form like bellow:

<form action="@Url.Action("ApiUploadImage","PP")" enctype="multipart/form-data" method="POST" id="frmObservationScoring"> 

Upvotes: 1

Related Questions