Receive images in ASP.Net Core and save to Entity Framework

Asked

Viewed 1,243 times

1

Good afternoon,

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

I have the html page that tries 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 don’t know how to get that image on, and save it in the bank with Entity.

I believe it’s something like :

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

Could someone help me ?

  • The answer worked out?

2 answers

2


From what I could see the code could receive 1 or several photos, but in your html failed to declare what the attribute is multiple in the input type file, alteration:

<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 however is want to write to the database, would then write the array de bytes or the way? If it is the array de bytes the code goes 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 going to write to a directory it is nice to just use the value of the file and have the path written in the table of the database and the file in some directory of your preference, example:

[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 became vague, if you put more information I edit this part and put its reality, but, already serves as a basis.

References

  • Hello Virgilio sorry the delay to reply, but then, Eduardo’s reply, seemed better in terms of processing, I do not know if I’m right to say this ... but I’m having trouble using an input file ... ?

  • Look what you said: Olá Virgilio desculpe a demora para responder, mas então, a resposta do Eduardo, me pareceu melhor em termos de processamento, não sei se estou certo ao dizer isto .. If it is not right, you cannot even state the code that is in my answer is what is in the documentation of aspnet core and realize that it is a class that has the name and characteristics that is for reading a given array of bytes. About the new question I know, but here at Stackoverflow answers a question, then you can ask another question and so on your question has two answers

  • ... continuing and you have to test the two forms and credit and accept as answer any of them, after that you are ready to ask another question, about Viewmodel has several questions and answer on the site itself, may be directed to some of them or made a new one if it does not have the same purpose. OK! @John-Jones ...

  • 1

    Virgilio worked with your explanation, sorry for my lack of knowledge on the subject, I’m still new in programming rs... Thank you for your patience and for helping me, strong embrace.

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 only save in bank normally

  • And instead of me doing it in the product class public byte[] Photo { get; set; } I would do something like public string Photo { get; set; } ? Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.