0

When I select a file and submit the file for upload, I can't get the value of the File path in my Model. In the Controller it shows as null. What am I doing wrong?

View

<form method="post" action="/Account/Profile" enctype="multipart/form-data">
    <label>Load photo: </label>
    <input type="file" name="filePath" id="file" />
    <input type="submit" value="submit" />
</form>

Controller

public ActionResult Profile(ProfileModel model, FormCollection form)
{
    string path = Convert.ToString(model.FilePath);
    return View();
}

Model

public HttpPostedFileBase FilePath
{
    get
    {
        return _filePath;
    }
    set
    {
        _filePath = value;
    }
}

public bool UploadFile()
{
    if (FilePath != null)
    {
        var filename = Path.GetFileName(FilePath.FileName);
        FilePath.SaveAs(@"C:\" + filename);
        return true;
    }
    return false;
}
George Stocker
  • 57,289
  • 29
  • 176
  • 237
r.r
  • 7,023
  • 28
  • 87
  • 129
  • Is it really possible to have data-binding for an file-upload-element? Currently I would say no... – OlafW Aug 24 '10 at 12:40

3 Answers3

2

I don't think model binding works with HttpPostedFileBase...

It should work if you take it out of your ViewModel and do it like this:

public ActionResult Profile(HttpPostedFileBase filePath)
{
    string path = Convert.ToString(filePath);
    return View();
}

HTHs,
Charles

Ps. This post could help explain things: ASP.NET MVC posted file model binding when parameter is Model

Community
  • 1
  • 1
Charlino
  • 15,802
  • 3
  • 58
  • 74
0

I don't have VS to simulate your problem. So im not sure bout the answer.

Try this, might work

<input type="file" name="model.FilePath" id="file" />

If not work then, Try look it on your formcollection & HttpContext.Request.Files

It should be there.

Petrick Lim
  • 91
  • 1
  • 7
  • then maybe you can't bind file upload element on a model paramater as commented by Olaf. I tried to look on my previous project code & i used HttpContext.Request.Files instead of binding it to model. – Petrick Lim Aug 25 '10 at 11:31
0

Instead of using HttpPostedFileBase I would use IFormFile

public class ModelFile
{
     .....
     public string Path{ get; set; }
     public ICollection<IFormFile> Upload { get; set; }
}


public async Task<IActionResult> Index([Bind("Upload,Path")] ModelFile modelfile)
{
     ...
     msg = await storeFilesInServer(modelfile.Upload,modelfile.Path);
}


private async Task<Message> storeFilesInServer(ICollection<IFormFile> upload, string path)
        {
            Message msg = new Message();
            msg.Status = "OK";
            msg.Code = 100;
            msg.Text = "File uploaded successfully";
            msg.Error = "";
            string tempFileName = "";
            try
            {
                foreach (var file in upload)
                {
                    if (file.Length > 0)
                    {
                        string tempPath = path + @"\";
                        if (Request.Host.Value.Contains("localhost"))
                            tempPath = path + @"\"; 

                        using (var fileStream = new FileStream(tempPath + file.FileName, FileMode.Create))
                        {
                            await file.CopyToAsync(fileStream);
                            tempFileName = file.FileName;
                        }
                    }
                    msg.Text = tempFileName;
                }
            }
            catch (Exception ex)
            {
                msg.Error = ex.Message;
                msg.Status = "ERROR";
                msg.Code = 301;
                msg.Text = "There was an error storing the files, please contact support team";
            }

            return msg;
        }