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
58k29 gold badges184 silver badges239 bronze badges
asked Aug 24, 2010 at 8:53
1
  • Is it really possible to have data-binding for an file-upload-element? Currently I would say no... Commented Aug 24, 2010 at 12:40

3 Answers 3

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

answered Aug 24, 2010 at 16:34
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Aug 24, 2010 at 9:10

1 Comment

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.
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;
 }
answered Jun 22, 2017 at 19:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.