1

Like you see in my diagram each Post can have multiple PostImages.

enter image description here

How can I upload images in ASP.NET MVC? I don't know how many images can have each post, so I don't like my approach:

 <label for="file1">Filename:</label>
 <input type="file" name="files" id="file1" />
 <label for="file2">Filename:</label>
 <input type="file" name="files" id="file2" />

And after I uploaded an Image I want to have a preview of the image,

This is what I have so far.

 @using (Html.BeginForm("Edit", "BlogPost", FormMethod.Post, new {   nctype="multipart/form-data" }))
 {
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true)

<fieldset>
    <legend>Post</legend>

    @Html.HiddenFor(model => model.ID)

    <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div>
    <label for="file1">Filename:</label>
    <input type="file" name="files" id="file1" />  

    <label for="file2">Filename:</label>
    <input type="file" name="files" id="file2" />

    <input type="submit"  />
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

Controller:

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult Edit(Post post, int[] selectedCategories, IEnumerable<HttpPostedFileBase> files)
 {
    if (ModelState.IsValid)
    {
       //Code to Edit
    }
 }
danielschemmel
  • 10,885
  • 1
  • 36
  • 58
POIR
  • 3,110
  • 9
  • 32
  • 48

1 Answers1

-1

Use the BlueImp jQuery File Uploader solution: https://blueimp.github.io/jQuery-File-Upload/ It provides:

"..multiple file selection, drag & drop support, progress bars, validation and preview images"

Ciaran
  • 543
  • 5
  • 14
  • The question seems to be about C# and ASP.NET, I'm not sure how jQuery can help there. – Amndeep7 Aug 16 '17 at 16:43
  • I've used it several times as a file uploader component of larger C# ASP.NET MVC applications. 'MVC' is irrelevant to uploading images and presenting a preview which is the OP's actual requirement. – Ciaran Aug 17 '17 at 08:54