Hi I seem to be having a problem when posting images.I have checked many questions on stackoverflow and on other forums that discuss this topic but none seem to provide the answer I need.Here is my code:
@using( Html.BeginForm("Create", "ProductManager", FormMethod.Post, new{enctype = "multipart/form-data"})){
<ul>
....
<li>
@Html.LabelFor(model => model.ProductImagePath , "Avatar")
<input type="file" id="ProductAvatar" name="ProductAvatar" />
@Html.HiddenFor(model => model.ProductImagePath , new { id = "AvatarHiddenField"})
</li>
<li>
@Html.LabelFor(model => model.ProductName , "Product Name")
@Html.EditorFor(model => model.ProductName)
</li>
.....
</ul>
}
[HttpPost]
public ActionResult Create( FormCollection collection , HttpPostedFileBase avatar)
{
string file = collection["ProductAvatar"];
var avatars = avatar;
}
From debugging I found that HttpPostedFileBase returns null.The other form data in the collection gets posted succesfully.Only the image does not get posted.I can not seem to acces ProductAvatar from either the FormCollection or the HttpPostedFileBase , it seems like it’s not even posted
How can I corect this problem?
You have to use change the name of your
HttpPostedFileparameter to the same name of your input file on the form, or you also can useRequest.Filesand get by the by thenameattribute of your input file, try something like this:The
nameattribute is what the browser will send on a post/get form when submit.