9
I am trying to upload images to my application. I have made a prototype upload, but it does not show the image, but its ID or NAME. And I’m uploading it to a folder I created in my project.
My doubts are:
How do I show this image?
In the project that I am working on, I need to show this image to the user, because it is a school and needs to have the student’s photo. I’ve even created the student register;
How to link this photo to the registration of the student in question?
Should I create a student model and another image model, or has some other form?
And what is the best way to upload, saving the image to the bank or to a folder in the project?
Here are my prototype codes:
Image.Cs (Model)
public partial class Imagem
{
public int ID { get; set; }
public string ImagePath { get; set; }
}
Imagemcontroller.Cs(Controller, and here only the create part)
public ActionResult Create()
{
return View();
}
// POST: /Imagem/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Imagem img, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
file.SaveAs(HttpContext.Server.MapPath("~/Imagens/")
+ file.FileName);
img.ImagePath = file.FileName;
}
db.Imagems.Add(img);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(img);
}
Index.cshtml(a View)
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.ImagePath)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
@* <img src="@Url." alt="Image" width="300px" /> *@
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new {id = item.ID})
</td>
</tr>
}
</table>
Create.cshtml(Other View)
@using (Html.BeginForm("Create", "Imagem", null, FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Image</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ImagePath)
</div>
<div class="editor-field">
<input id="ImagePath" title="Upload a product image"
type="file" name="file" />
</div>
<p><input type="submit" value="Create" /></p>
</fieldset>
}
Really good guy, really good. But in this case, I would need to have in the controller the Delete, Update and Details ? I mean, if the user wants to change the image, he would change it in a good way ?
– Érik Thiago
Yes, I would change in a good way, the example is for you to have idea with would be a create and Edit, but, the other methods are not mandatory
– user6026
Oh I got it. So Create and Edit would be the same way you put it there ? Because I’m afraid the directory is too full of images. I mean, the user only creates one on top of the other, never deletes the old photo.
– Érik Thiago
You can delete the old photo yes on Edit... but if the names match it overwrites the last photo from that record
– user6026
I get it, and if the name is the same, he keeps the two photos ? The old and the new right ?
– Érik Thiago
I changed the code to delete if Edit comes with photo!
– user6026
Thank you so much for your patience and for your willingness to help me. Look worked right, the way I wanted. I wanted to thank you so much ! Thanks for the help @Fccdias!
– Érik Thiago
The file is doing what in the code is that here gives me error
– Leadnnd Pereira