Note that there are some explanatory texts on larger screens.

plurals
  1. POIn MVC4, how do I upload a file (an image) to SQL Server that's part of my domain model?
    primarykey
    data
    text
    <p>I'm relatively new to MVC, and I've never had to deal with uploading a file (an image, specifically) to a SQL Server database. To be honest, I don't know what I'm doing here. </p> <p>Here's what I have so far - here's my domain model (note the <code>HttpPostedFileBase</code> in my model - this is what I want to upload):</p> <pre><code>public class Profile { [Key] public int Id { get; set; } [Required(ErrorMessage="Years of service is required")] [DisplayName("Years Service:")] public int YearsService { get; set; } [DataType(DataType.MultilineText)] [DisplayName("Notable Achivements:")] public string NotableAchivements { get; set; } [Required(ErrorMessage = "Technical skills are required")] [DataType(DataType.MultilineText)] [DisplayName("Technical Skills:")] public string TechnicalSkills { get; set; } [DisplayName("Upload Image: ")] public HttpPostedFileBase Photo { get; set; } public string CreatedBy { get; set; } public DateTime CreatedDate { get; set; } public string ModifiedBy { get; set; } public DateTime ModifiedDate { get; set; } } </code></pre> <p>And here's my view:</p> <pre><code>@using (Html.BeginForm("Create", "Profiles", FormMethod.Post, new { enctype = "multipart/form-data" })) { &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.YearsService) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.YearsService) @Html.ValidationMessageFor(model =&gt; model.YearsService) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.NotableAchivements) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.NotableAchivements) @Html.ValidationMessageFor(model =&gt; model.NotableAchivements) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.TechnicalSkills) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.TechnicalSkills) @Html.ValidationMessageFor(model =&gt; model.TechnicalSkills) &lt;/div&gt; &lt;input type="file" name="photo" /&gt; &lt;input type="submit" name="Submit" id="Submit" value="Upload" /&gt; } </code></pre> <p>I hope there's something glaringly obvious that I'm doing wrong. Can anyone provide advice on how to do a simple file upload to a SQL Server database?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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