0
How do I get a received image in a Form HTML
and write to the bank in VARBINARY
with C#
?
I’m using C# ASP.NET MVC5 CORE
.
0
How do I get a received image in a Form HTML
and write to the bank in VARBINARY
with C#
?
I’m using C# ASP.NET MVC5 CORE
.
1
On your model, you need an Iformfile type property:
public class MyModel
{
public IFormFile Arquivo {get;set;}
}
In your object, or POCO class, you need a byte property[]:
public class MyObject
{
public byte[] Foto {get;set;}
}
And In the action of your controller, you take the Model, pass it to stream and then to byte[] and then save it to the bank:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Upload(MyModel model)
{
if (ModelState.IsValid)
{
MyObject obj = new MyObject();
using (var memoryStream = new MemoryStream())
{
await model.Arquivo.CopyToAsync(memoryStream);
//Recebo o stream, passo pra bytearray
byte[] barray = memoryStream.ToArray();
//passo o array para o objeto
obj.Foto = barray;
}
await _context.MyObjects.AddAsync(obj);
_context.SaveChanges();
}
return View("Index");
}
Do not forget to add the enctype="Multipart/form-data" property in the form I hope I’ve helped.
Edit. I put it first to record as a string (varchar) and not varbinary. Fixed.
cannot create an Iformfile type property in my Model. VS does not recognize and enables no fix suggestion.
Are you sure you’re using Asp.Net Core ? https://docs.microsoft.com/pt-br/aspnet/core/mvc/models/file-uploads https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.iformfile?view=aspnetcore-2.0
Yes. In the document you attached, it tells you to create an Iformfile class and not a property. This proceeds, or I have to create a property within an existing class that is not an interface?
I did exactly the way I posted it, it works perfectly. Obviously there are several other ways to do it. If you are not or want to use a model with properties, set Iformfile as the action parameter
0
I tried to use the code:
[HttpPost]
public ActionResult UploadFile(HttpPostedFileWrapper file)
{
FileStream fs = new FileStream(Convert.ToString(file), FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] imagemArray = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return View("UploadFile");
}
but I don’t know which type the action has to receive the HTML file, I tried string and Httppostedfilebase, but it doesn’t work.
Browser other questions tagged c# html asp.net-mvc-5 image varbinary
You are not signed in. Login or sign up in order to post.
Share your code where you configure the VARBINARY field. Another tip, if the files are larger than 1MB, it is more interesting to use FILESTREAM.
– Gabriel Coletta
@Gabrielcoletta the bank is already created with the VARBINARY field and the files will not exceed 1mb, are miniature logo images of companies. Bank fields: Cd_enterprise(int), Ds_enterprise(VARCHAR(60)) and Ds_logo(VARBINARY)
– Diego Grossi
@Diegogrossi resolved ?
– Rovann Linhalis
No @Rovannlinhalis, I had to save in a directory and recorded the image path in the bank. When I call the image picked up the path in the bank.
– Diego Grossi
Write a response to your solution and share it with the community, vlw
– Rovann Linhalis