0

I have a field called photo at my table user. I want to know if this possible to save a image with only this column called photo(it's a string). I want only to save the path of the image that was uploaded by a user, and show it later. Is it possible? Can anyone show an example?

Ryan Santos
  • 101
  • 5

1 Answers1

0

yes, you can. Here is similar question. Just update user photo column with web-server relative path and show it later in view.

To display uploaded image witch was saved to server file system and it's absolute (relative) url was saved to database (or elsewere) you must return to view model containing relative to your web root url of your uploaded image and show it using html or css.

<img src='@Url.Content(Model.imageUrl)' />
<div style='background-image:url(@Url.Content(Model.imageUrl))'></div>

Check your action Request.Files collection:

public ActionResult Adicionar(usuario usuario)
{
    if (Request.Files.Count > 0)
    {
        for (int i = 0; i < Request.Files.Count; i++)
        {
        //do whatever you want.
        }
    }
}
Community
  • 1
  • 1
aleha_84
  • 8,309
  • 2
  • 38
  • 46
  • I'm getting this error System.NullReferenceException at this line string pic = System.IO.Path.GetFileName(file.FileName); – Ryan Santos Sep 15 '14 at 13:13
  • @Ryan Santos have you checked file for null? – aleha_84 Sep 15 '14 at 13:49
  • Yes! When but when i do this the error passes to the line file.InputStream.CopyTo(ms); – Ryan Santos Sep 15 '14 at 13:53
  • @Ryan Santos you need byte array only if you want to store your image in db. Try remove variable 'file' from function argument and check Request.Files collection. If you submit form with selected file, then you should receive it in action. – aleha_84 Sep 15 '14 at 14:07
  • Hi, sorry for the wait, i was at work. I can make the insert on database, but i only see the name of the image. How can I display the image itself? – Ryan Santos Sep 16 '14 at 01:40
  • I Will try when a i get home. But I think i Will have to Add one more column to save the path of the image – Ryan Santos Sep 16 '14 at 17:32
  • @RyanSantos you said that you already have column called 'photo'. Why another one? – aleha_84 Sep 16 '14 at 18:13
  • I think it's because the photo column is saving only the name of the image and not the path What I find strange is the fact that the photo that was uploaded does not appear at the folder... – Ryan Santos Sep 16 '14 at 18:43
  • public ActionResult Adicionar(usuario usuario, HttpPostedFileBase fotoperfil){ if (ModelState.IsValid) { if (fotoperfil != null) { string imagem = Path.GetFileName(fotoperfil.FileName); string caminho = Path.Combine(Server.MapPath("~/Fotoperfil/" + imagem)); fotoperfil.SaveAs(caminho); usuario.fotoperfil = caminho; } db.usuario.Add(usuario); db.SaveChanges(); return RedirectToAction("Index"); } return View(usuario); } – Ryan Santos Sep 16 '14 at 23:22
  • you saving in db absolute path to foto. in common cases you should store relative to server parh there. Also place breakpoint in this method and check all variables to check where file saving. – aleha_84 Sep 17 '14 at 03:44
  • I think it's something wrong with the httppostedfilebase fotoperfil. because this fotoperfil is getting null, but the one in usuario is not. I think i will try to separate the methods. – Ryan Santos Sep 17 '14 at 04:51
  • no. Take a look at updated my ansver. Try check Request.Files collection. – aleha_84 Sep 17 '14 at 07:47
  • hm. I put the request file code, and now when I try to upload a image it says that it's not valid. – Ryan Santos Sep 17 '14 at 13:36