Reputation: 2495
I want to use fileUpload in my MVC view which is an .aspx page. I want to fetch the path into an object in my Controller.
<% using (Html.BeginForm("AddClient","Client",FormMethod.Post,new {enctype ="multipart/form-data" }))
{ %>
<div class="float-left">
<%: Html.Label("BAAFile Path") %>
<br />
<input type="file" name="file" id="file" />
<%: Html.ValidationMessageFor(model => model.BAAFilePath) %>--%>
</div>
<% } %>
at the bottom of the page there is a button to Save details(no button beside fileupload control).
Now i want to fetch the path of fileupload control in to my controller.
[HttpPost]
[ButtonName(Name = "Save")]
public ActionResult AddClient(ClientService.ClientDto client,HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
}
}
now i want to fetch the filepath
var fileName = Path.GetFileName(file.FileName);
into object of "ClientService.ClientDto" which is "client".
client.BAAFilePath=fileName
but iam getting null data into "var fileName".
iam using following code in "ActionResult AddClient(ClientService.ClientDto client)"
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
}
am i missing anything?
Upvotes: 0
Views: 463
Reputation: 37
I would suggest binding your form to a view model and then your view model to DTO. Your view model should have a property "File" of type HttppostedFileBase, you'll then get everything bound properly when posting your form. Make sure your form has the enctype="multipart/form-data"
Upvotes: 1