Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.Web.HttpPostedFileWrapper string is being saved instead of file pathname
    text
    copied!<p>This used to work fine, but now the code is not saving the pathname to the <code>SitePlan</code> and <code>SiteImage</code> fields. If I debug everything looks fine and <code>SitePlan</code> and <code>SiteImage</code> contain pathnames to the uploaded files (i.e. ~/UploadedFiles/20110210104108SiteImage77.jpg). But once save in the <code>SitePlan</code> and <code>SiteImage</code> fields is the string value "System.Web.HttpPostedFileWrapper".</p> <p>Struggling with this since its saves fine and watching and debugging the value being saved is the pathname so no error and all appears to work, just the database has no path, just this string "System.Web.HttpPostedFileWrapper". Any comments greatly appreciated</p> <p>Here is my controller code :</p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] public ActionResult SiteLocationEdit(int id, FormCollection collection) { SiteLocation siteLocation = this._siteRepository.GetSite(Convert.ToInt16(collection["SiteId"])).SiteLocation; if (Request.Files.Count &gt; 0 &amp;&amp; Request.Files["SitePlan"].ContentLength &gt; 0) { DeleteFile(siteLocation.SitePlan); siteLocation.SitePlan = SaveFile(Request.Files["SitePlan"], @"~/UploadedFiles", "SitePlan" + siteLocation.SiteId.ToString()); } if (Request.Files.Count &gt; 0 &amp;&amp; Request.Files["SiteImage"].ContentLength &gt; 0) { DeleteFile(siteLocation.SiteImage); siteLocation.SiteImage = SaveFile(Request.Files["SiteImage"], @"~/UploadedFiles", "SiteImage" + siteLocation.SiteId.ToString()); } TryUpdateModel(siteLocation); if (!ModelState.IsValid) return View(siteLocation); this._siteRepository.Save(User.Identity.Name); return RedirectToAction("SiteLocationDetails", new { id = siteLocation.SiteId }); } </code></pre> <p>Here is my View containing a Partial View (shown later in this post)</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;Lms.Model.SiteLocation&gt;" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt; &lt;%= Html.Encode(Model.Site.SiteDescription) %&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;%=Html.Script("~/Scripts/jquery.textarea-expander.js")%&gt; &lt;%= Html.ValidationSummary("Location Create was unsuccessful. Please correct the errors and try again.\n If you uploaded images during this update please upload again.")%&gt; &lt;% using ( Html.BeginForm ( "SiteLocationEdit", "Site", FormMethod.Post, // add an encoding type attribute // that is required for file upload new { enctype = "multipart/form-data" } ) ) {%&gt; &lt;% Html.RenderPartial("SiteTabs", Model.Site); %&gt; &lt;div class="clear"&gt; &lt;/div&gt; &lt;% Html.RenderPartial("SiteLocationForm", Model); %&gt; &lt;% } %&gt; &lt;script type="text/javascript"&gt; /* jQuery textarea resizer plugin usage */ $(document).ready(function() { jQuery("textarea[class*=expand]").TextAreaExpander(); // initialize all expanding textareas, new code, john s 10/08/2010 }); &lt;/script&gt; &lt;/asp:Content&gt; </code></pre> <p><strong>Here is the partial view :</strong></p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Lms.Model.SiteLocation&gt;" %&gt; &lt;fieldset&gt; &lt;div&gt; &lt;h3&gt; &lt;label id="Label2" style="text-align: left"&gt; &lt;%= "Site Location: " + Html.Encode(Model.Site.SiteDescription) %&gt;&lt;/label&gt; &lt;/h3&gt; &lt;/div&gt; &lt;div class="formFields"&gt; &lt;ul&gt; &lt;%--This is used to identify the site object to update when the model is returned to the controller--%&gt; &lt;%= Html.Hidden("SiteId", Model.SiteId)%&gt; &lt;li&gt; &lt;label for="Latitude"&gt; &lt;strong&gt;Latitude:&lt;/strong&gt;&lt;/label&gt; &lt;%= Html.TextBox("Latitude")%&gt; &lt;%= Html.ValidationMessage("Latitude", "*")%&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="Longitude"&gt; &lt;strong&gt;Longitude:&lt;/strong&gt;&lt;/label&gt; &lt;%= Html.TextBox("Longitude") %&gt; &lt;%= Html.ValidationMessage("Longitude", "*") %&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="Location"&gt; &lt;strong&gt;Location Address:&lt;/strong&gt;&lt;/label&gt; &lt;label&gt;&lt;%= Html.TextArea("Location", Model.Location, new { @class = "expand50-200"}) %&gt;&lt;/label&gt; &lt;%= Html.ValidationMessage("Location", "*") %&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="NearestPostcode"&gt; &lt;strong&gt;Nearest Postcode:&lt;/strong&gt;&lt;/label&gt; &lt;%= Html.TextBox("NearestPostcode") %&gt; &lt;%= Html.ValidationMessage("NearestPostcode", "*") %&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="TimeFromOfficeToTurbine"&gt; &lt;strong&gt;Office to Windfarm (Time):&lt;/strong&gt;&lt;/label&gt; &lt;%= Html.TextBox("TimeFromOfficeToTurbine") %&gt; &lt;%= Html.ValidationMessage("TimeFromOfficeToTurbine", "*") %&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="Directions"&gt; &lt;strong&gt;Comments:&lt;/strong&gt;&lt;/label&gt; &lt;label&gt;&lt;%= Html.TextArea("Directions", Model.Directions, new { @class = "expand50-200" })%&gt; &lt;/label&gt; &lt;%= Html.ValidationMessage("Directions", "*") %&gt; &lt;/li&gt; &lt;li&gt; &lt;h5&gt;&lt;strong&gt;For Image Uploads:&lt;/strong&gt; Please use only JPG,JPEG or GIF formats. Image size should be appropriate for the webpage to display, approximately 500x375 (WidthxHeight) &lt;/h5&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="Site Plan"&gt; &lt;strong&gt;Site Plan:&lt;/strong&gt;&lt;/label&gt; &lt;input id="SitePlan" name="SitePlan" type="file" /&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="Site Image"&gt; &lt;strong&gt;Site Image:&lt;/strong&gt;&lt;/label&gt; &lt;%-- &lt;%//= Html.TextBox("SiteImage", Model.SiteImage)%&gt; &lt;%//= Html.ValidationMessage("SiteImage", "*") %&gt;--%&gt; &lt;input id="SiteImage" name="SiteImage" type="file" /&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;div class='demo'&gt; &lt;%=Html.ActionLink("&lt; Back to List", "Index") %&gt;&lt;input type="submit" value="Save" /&gt; &lt;/div&gt; </code></pre> <p>and here is the save file procedure in the controller code :</p> <pre><code>protected String SaveFile(HttpPostedFileBase file, String path, string name) { if (file != null &amp;&amp; file.ContentLength &gt; 0) { if (path == null) { throw new ArgumentNullException("path cannot be null"); } string fileType = file.FileName.Substring(file.FileName.LastIndexOf("."), file.FileName.Length - file.FileName.LastIndexOf(".")); String relpath = String.Format("{0}/{1}", path, PrefixFName(name + fileType)); try { file.SaveAs(Server.MapPath(relpath)); return relpath; } catch (HttpException e) { throw new ApplicationException("Cannot save uploaded file", e); } } return null; } </code></pre> <p>Here is the SiteLocationCreate() THIS WORKS, just the EDIT that does not :</p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] public ActionResult SiteLocationCreate(SiteLocation siteLocation, FormCollection collection) { // TODO CF: Look into this line. Is there a better way to do it? I would think so. // It uses a hidden field in the object form Site site = this._siteRepository.GetSite(Convert.ToInt16(collection["SiteId"])); site.SiteLocation = siteLocation; if (Request.Files.Count &gt; 0 &amp;&amp; Request.Files["SitePlan"].ContentLength &gt; 0) { DeleteFile(siteLocation.SitePlan); siteLocation.SitePlan = SaveFile(Request.Files["SitePlan"], @"~/UploadedFiles", "SitePlan" + siteLocation.SiteId.ToString()); } if (Request.Files.Count &gt; 0 &amp;&amp; Request.Files["SiteImage"].ContentLength &gt; 0) { DeleteFile(siteLocation.SiteImage); siteLocation.SiteImage = SaveFile(Request.Files["SiteImage"], @"~/UploadedFiles", "SiteImage" + siteLocation.SiteId.ToString()); } if (!ModelState.IsValid) return View(siteLocation); this._siteRepository.Save(User.Identity.Name); return RedirectToAction("SiteLocationDetails", new { id = site.SiteId }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload