Reputation: 10669
In my view model I've added an HttpPostedFileBase
object as a property. On my part of my view, I want people to be able to select a file from anywhere on our LAN and be able to upload a spreadsheet for processing.
The page as I have it written write now posts back to the correct controller method as long as no file has been selected. However, if I select a spreadsheet to send back to the controller I get the page displayed below from Chrome. I've set a breakpoint in the controller just to verify, and it only gets hit if I do not select a file.
ViewModel
namespace Monet.ViewModel
{
public class ZipCodeIndex
{
//Imported spreadsheet
[DisplayName("Import Spreadsheet")]
public HttpPostedFileBase Import { get; set; }
View
@model Monet.ViewModel.ZipCodeIndex
@{
ViewBag.Title = "Zip Code Territory Search";
Layout = "~/Views/Shared/_Layout.cshtml";
}
using (Html.BeginForm("UploadFile", "ZipCodeTerritory", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ActionLink("Create New", "Create")
<div style="float: right; padding-left: 15px;">
@Html.TextBoxFor(model => model.Import, new { type = "file" })
<input type="submit" value="Upload Spreadsheet" />
</div>
}
Controller
[HttpPost]
public ActionResult UploadFile(ZipCodeIndex index)
{
//Set a breakpoint here that only gets hit if file isn't selected
var fp = Path.Combine(HttpContext.Server.MapPath("~/ImportUploads"), Path.GetFileName(index.Import.FileName));
return RedirectToAction("Index");
}
EDIT
I altered the form to simply pass the file back to the controller. However, I'm still encountering the exact same issue. Using the code below the site will post back to the controller if there is not file selected. However, if a file is selected I receive the "webpage is not available" message (get this using Chrome, IE 10, and Firefox).
View
using (Html.BeginForm("UploadFile", "ZipCodeTerritory", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ActionLink("Create New", "Create")
<div style="float: right; padding-left: 15px;">
<input type="file" name="FileUpload1" />
<input type="submit" value="Upload Spreadsheet" />
</div>
}
Controller
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase Import)
{
//Set a breakpoint here that only gets hit if file isn't selected
var fp = Path.Combine(HttpContext.Server.MapPath("~/ImportUploads"), Path.GetFileName(Import.FileName));
return RedirectToAction("Index");
}
Here is a screenshot of the Network tab in the Developer Tools
SECOND EDIT
Ok, so I have found that any of the methods posted either in my post or below actually work as long as I'm not grabbing the spreadsheet I need... I grabbed a simple text file and was able to post the file back to the controller just fine. Next I tried a file a little bit bigger, and bigger until I went for the spreadsheet that I need for this page (the spreadsheet has 100k records in it). Once I got to the spreadsheet I received the "Webpage is not available" message again.
Is there a file size limit when using an HttpPostedFileBase
and if so is there a way to alter it, say in the web.config?
The spreadsheet I will typically need is going to be around 9/10 MB large.
Upvotes: 1
Views: 1098
Reputation: 11673
Try increasing the MaxRequestLength size, add this to your web.config under the <system.web>
section.
<httpRuntime maxRequestLength="1048576" />
Add the attribute if the 'httpRuntime' element already exists. Can read more about this here.
Upvotes: 2
Reputation: 218732
Is your UploadFile
action method is in ZipCodeTerritory
controller (ZipCodeTerritoryController.cs
) ? If yes, This should work.
@using (Html.BeginForm("UploadFile", "YourControllerNameHere",
FormMethod.Post, new { enctype = "multipart/form-data" }))
{
//your code
}
One reason you may get the "Page not found" could be, you do not have the Index action in your current controller (ZipCodeTerritoryController
) to which you are redirecting the user.
Upvotes: 0
Reputation: 16719
I'm not sure that you can embed the HttpPostedFileBase
in a view model, but I am fairly certain that you cannot use this:
@Html.TextBoxFor(model => mode.Import, new { type = "file" })
Instead, you have to use an input type="file"
and name it the same thing as the controller expects:
<input type="file" name="Import" />
If it still doesn't bind, I would take it out of the view model and change your POST action to something like the following:
public ActionResult UploadFile(ZipCodeIndex index, HttpPostedFileBase import)
Again, I don't know if you can bind HttpPostedFileBase
within a view model, but I do know that the second method works as long as you name your input
element the same as your parameter is named.
Upvotes: 0