I have the following image upload code and controller. The hidden ID is POSTing successfully, but the image remains null.
The form:
#using(Html.BeginForm()){
<input type="hidden" name="merchandiseId" id="id" value="#ViewBag.Id"/>
<input type="file" name="image" id="image" />
<input type="submit" />
}
The controller:
[HttpPost]
public ActionResult AddImage(int merchandiseId, HttpPostedFileBase image)
Debugging and stepping in verifies that image is null while merchandiseId has the correct value.
You need to add the enctype = "multipart/form-data" in your form. otherwise no file will be uploaded.
An example
#using (Html.BeginForm("UploadAction", "MyController", new { Model.Id }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="myFile">
<button type="submit>Upload</button>
}
Try adding new { enctype = "multipart/form-data" }to your form
Related
I am passing in a reference to a file, and a string value cardid. For some reason when I return the result the cardid value is null. However when I view the page the <h3> header contains the string I expected. What am I doing wrong?
HTML view
<h3>#Model.CV.Id</h3>
#using (Html.BeginForm("UploadFile", "Tickets", FormMethod.Post, new { cardid=#Model.CV.Id, enctype = "multipart/form-data"}))
{
<input type="file" name="file" />
<input type="submit" value="Upload Attachment" />
}
Controller
public async Task<IActionResult> UploadFile(string cardid,IFormFile file)
{
......
string res="yay";
return Ok(new {cardid=cardid,res=res });
}
You should include cardid in your form. You are simply creating an attribute on <form>, which does nothing.
<h3>#Model.CV.Id</h3>
#using (Html.BeginForm("UploadFile", "Tickets", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
<input type="hidden" name="cardid" value="#(Model.CV.Id)" />
<input type="file" name="file" />
<input type="submit" value="Upload Attachment" />
}
You could also include `cardid in the action:
Html.BeginForm("UploadFile?cardid=" + Model.CV.Id, "Tickets", FormMethod.Post...)
The following code works:
#using (Html.BeginForm("MyAction", "MyController", FormMethod.Post, new { enctype = "multipart/form-data", data_ajax = "false" }))
{
#Html.AntiForgeryToken()
<input type="file" name="UploadedFile" />
<input type="submit" value="Upload" class="btn btn-primary" />
}
If I do Request.Files.Count I'll have 1 file in (given that I actually submitted a file in the form, of course), but if I change the data_ajax attribute to true, I never get a file (count is 0), neither through the Request.Files.Count, not through a parameter (where the list would be null) (ie:)
public ActionResult MyAction(IEnumerable<HttpPostedFileBase> files) {...}
Can anyone explain to me why does one actually submit the right form (with a file), but the other excludes the file from the request?
Salaamun Alekum
I Am Getting NULL in file instance of action after posting a file in FORM
public ActionResult CreateDoctorProfile(HttpPostedFileBase file)
{
int LoggedInPersonID = Convert.ToInt32(Session["LoggedInPersonId"]);
erx_t_personnel PersonnelInformation = db.erx_t_personnel.Where(PersonnelInformation1 => PersonnelInformation1.Person_Id == LoggedInPersonID).FirstOrDefault();
return View(PersonnelInformation);
}
This Is My View
#model Doctors_Search_Engine.Models.erx_t_personnel
#{
ViewBag.Title = "Personnel Registration";
}
<h2>Personnel Registration</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<input type="file" name="file" />
<input type="submit" value="Create" class="btn btn-default" />
}
What Should I Do To Get Value In file Of Action Parameters. I Need Guidance On This
Thank You
The problem you have and the subject of your question are totally different. It's confusing.
If you are having an issue passing the file to your controller then your input file name should match the parameter in your controller method.
Change <input type="file" name="Image" /> to <input type="file" name="file" />
Or change your action result to:
public ActionResult CreateDoctorProfile(HttpPostedFileBase Image){}
You also need to set the encryption type to your form.
using (Html.BeginForm(new { enctype = "multipart/form-data" }))
I am using C# and ASP.NET MVC4 for a web application (mobile template).
Now I have the following code in my view:
#using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<label for="file">Upload:</label>
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload" />
}
and this in my controller:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
string path = System.IO.Path.Combine(Server.MapPath("~/Content"), System.IO.Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
return View();
}
when I run the application and try to upload a file, I get
"Object reference not set to an instance of an object." message in Visual Studio.
But I know that the code above works fine in ASP.NET MVC3 as I've used it before.
Can anyone help me with this?
Add this attribute to your form: data-ajax="false"
#using(Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype="multipart/form-data", data_ajax="false"})){
<label for="file">Upload:</label>
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload" />
}
You don't have to use HttpPostedFileBase, as the matter of fact you don't have to receive any parameters in your [HttpPost] Action. You can get your file from the request like this as long as your form is set to enctype = "multipart/form-data" (which you already do):
var file = Request.Files[0];
I have this simple piece of HTML code:
<div>
<input type="file" name="english-file" />
</div>
<div>
<input type="file" name="french-file" />
</div>
I used in my model, the following C# line code:
object receivedFiles = HttpContext.Current.Request.Params["english-file"];
but that object returns null always.
Of course that I can use HttpContext.Current.Request.Files but I want to sepparate files by languages (the above HTML code is simple but someone could add more files to english than french with +Add files button) and save them in sepparate table.
How could I do that?
That's not how files are uploaded. You should look in Request.Files["english-file"] after setting the proper enctype on the form.
Let's take an example:
#using (Html.BeginForm("upload", "somecontroller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<input type="file" name="english" />
</div>
<div>
<input type="file" name="french" />
</div>
<button type="submit">Upload</button>
}
and in the controller action:
[HttpPost]
public ActionResult Upload()
{
var englishFile = Request.Files["english"];
var frenchFile = Request.Files["french"];
...
}
or even better:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase english, HttpPostedFileBase french)
{
...
}
or even better:
[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
...
}
assuming you adjust the names of the file inputs:
#using (Html.BeginForm("upload", "somecontroller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<input type="file" name="files" />
</div>
<div>
<input type="file" name="files" />
</div>
<button type="submit">Upload</button>
}
I also invite you to read the following blog post.