Receiving images in ASP.Net Core and saving in the Entity Framework

1

Good afternoon,

Can anyone tell me how to get an image in ASP.Net Core?

I have the html page that you are trying to send to the server:

<form method="post" enctype="multipart/form-data" asp-controller="Produto" asp-action="ImageLoad">
<div class="form-group">
    <div class="col-md-10">
        <p>Upload one or more files using this form:</p>
        <input type="file" name="files"/>
    </div>
</div>
<div class="form-group">
    <div class="col-md-10">
        <input type="submit" value="Upload" />
    </div>
</div>
</form>

But I do not know how to get this image in, and save it on the bank with the entity.

I think it's something like:

[HttpPost]
public async Task ImageLoad(List<IFormFile> files)
{
    foreach (var file in files)
    {
        // do something
    }

Could anyone help me?

    
asked by anonymous 05.12.2017 / 17:27

2 answers

1

From what I could observe the code could receive 1 or several photos, but in its html it was missing to declare what is the multiple attribute in input type file , change:

<form method="post" enctype="multipart/form-data" 
      asp-controller="Produto" asp-action="ImageLoad">
<div class="form-group">
    <div class="col-md-10">
        <p>Upload one or more files using this form:</p>
        <input type="file" name="files" multiple/> // multiple
    </div>
</div>
<div class="form-group">
    <div class="col-md-10">
        <input type="submit" value="Upload" />
    </div>
</div>
</form>

Another one, however, is to write to the database, would it then write the array de bytes or the path? If it is array de bytes the code looks like this:

[HttpPost]
public async Task ImageLoad(List<IFormFile> files)
{
    byte[] arq = null;
    foreach (var file in files)
    {           
        using (BinaryReader reader = new BinaryReader(file.OpenReadStream()))
        {
            arq = reader.ReadBytes((int)file.Length);
        }           
        // operações de gravação e utilize
        // a variável file para mandar o valor para o tabela
    }
}

Now if you are to write to a directory it is very similar to just use the value of the file and have the path recorded in the database table and the file in some directory of your preference, eg

[HttpPost]
public async Task ImageLoad(List<IFormFile> files)
{
    foreach (var file in files)
    {
        file.CopyTo(new FileStream("/diretorio/name_do_arquivo", FileMode.Create));
    }
}

These are real forms, but, generic, because the context of the question has become vague, if you put more information I edit this part and put your reality, but it already serves as a basis.

05.12.2017 / 19:20
0
//Convert IFromFile para base64
  private string ConvertIFromFileToBase64(IFormFile file)
  {
      Stream stream = file.OpenReadStream();
      using (var memoryStream = new MemoryStream())
      {
           stream.CopyTo(memoryStream);
           return Convert.ToBase64String(memoryStream.ToArray());
      }
  }

Now just save to the bank normally

    
05.12.2017 / 17:53