Jump to content
MigaMG93

Upload Image c# asp.netcore.mvc

Recommended Posts

MigaMG93

Boa noite, estou a tentar dar upload de uma imagem e aparece-me sempre este erro:

NullReferenceException: Object reference not set to an instance of an object.

var caminho = Path.Combine(he.WebRootPath + "/ImageFiles",

 

O código é este:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Vinhoid,Nome,Ano,Teoralcool,Produtorid,Tipoid,Regiaoid,Datacriacao,Localimagem,Garrafid,Descricao,Preco,Stock")] VinhoDistribuicao vinhoDistribuicao, IFormFile Localimagem)
    {
        if (HttpContext.Session.GetString("TipoUtilizador") == "Garrafeira")




        {
            if (ModelState.IsValid)
            {
               if (_context.Vinho.Any(x => x.Ano == vinhoDistribuicao.Ano && x.Nome == vinhoDistribuicao.Nome && x.Tipoid == vinhoDistribuicao.Tipoid) == false)

               {

                    var caminho = Path.Combine(he.WebRootPath + "/ImageFiles",
                        Path.GetFileName(Localimagem.FileName));

                    FileStream fileStream = new FileStream(caminho, FileMode.Create);
                    Localimagem.CopyTo(fileStream);
                    fileStream.Close();

 

Html

    

@model Lab4_Vinhos6.Models.VinhoDistribuicao
...............
<div class="form-group">
                <label asp-for="Localimagem" class="control-label"></label>
                <input asp-for="Localimagem" type="file" accept=".png, .jpg" class="form-control" value="" />

                <span asp-validation-for="Localimagem" class="text-danger"></span>
            </div>


.............

Alguem me consegue ajudar?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.