Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. public ActionResult Create([Bind(Include = "ProdutoId,Nome,Descricao,Valor,CategoriaId,imagem")] Produto produto, int? id, HttpPostedFileBase file)
  2.         {
  3.             if (ModelState.IsValid)
  4.             {
  5.                 if (file != null)
  6.                 {
  7.                     string pic = System.IO.Path.GetFileName(file.FileName);
  8.                     string path = System.IO.Path.Combine(
  9.                                            Server.MapPath("~/Imagens/"), pic);
  10.                     // file is uploaded
  11.                     file.SaveAs(path);
  12.                     produto.imagem = pic.ToString();
  13.  
  14.                     // save the image path path to the database or you can send image
  15.                     // directly to database
  16.                     // in-case if you want to store byte[] ie. for DB
  17.                     using (MemoryStream ms = new MemoryStream())
  18.                     {
  19.                         //Aqui converter para Base64 caso queira salvar a imagem toda no banco
  20.                         file.InputStream.CopyTo(ms);
  21.                         byte[] array = ms.GetBuffer();
  22.                         //Faça o atributo ImagemArray receber a string convertida
  23.                         //img.ImagemArray
  24.                     }
  25.                 }
  26.  
  27.                 ProdutosDAO.Add(produto);
  28.                 return RedirectToAction("Index");
  29.             }
  30.  
  31.             ViewBag.CategoriaId = new SelectList(Singleton.Instance.Entities.Categorias, "CategoriaId", "CategoriaNome", produto.CategoriaId);
  32.             return View(produto);
  33.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement