Home > Mobile >  Cannot return null from an action method with a return type of 'Microsoft.AspNetCore.Mvc.IActio
Cannot return null from an action method with a return type of 'Microsoft.AspNetCore.Mvc.IActio

Time:12-18

Dears i am using asp.net core 3.1 the problem when i download it gives me this error (Cannot return null from an action method with a return type of 'Microsoft.AspNetCore.Mvc.IActionResult)

this the model

{
    public class Files
    {
        [Key]
        public int DocumentId { get; set; }
        public string Name { get; set; }
        public string FileType { get; set; }
        public byte[] DataFiles { get; set; }
    }
}

this the controller

namespace Info.Controllers
{
    public class DemoController : Controller
    {
        private readonly ApplicationDbContext _context;

        public DemoController(ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult Index()
        {
            var result = _context.Files.ToList();
            return View(result);
        }

        [HttpPost]
        public IActionResult Index(IFormFile files)
        {
            if (files != null)
            {
                if (files.Length > 0)
                {
                    //Getting FileName
                    var fileName = Path.GetFileName(files.FileName);
                    //Getting file Extension
                    var fileExtension = Path.GetExtension(fileName);
                    // concatenating  FileName   FileExtension
                    var newFileName = String.Concat(Convert.ToString(Guid.NewGuid()), fileExtension);

                    var objfiles = new Files()
                    {
                        DocumentId = 0,
                        Name = newFileName,
                        FileType = fileExtension,
                    };

                    using (var target = new MemoryStream())
                    {
                        files.CopyTo(target);
                        objfiles.DataFiles = target.ToArray();
                    }

                    _context.Files.Add(objfiles);
                    _context.SaveChanges();
                }
            }

            return RedirectToAction("Index");
        }

        //       public IActionResult DownloadImage(int id)
        //     {
        //         byte[] bytes;
        //          string fileName, contentType;

        //          var item = _context.Files.FirstOrDefault(c => c.DocumentId == id);

        //          if (item != null)
        //         {
        //              fileName = item.Name;

        //              contentType = item.FileType;
        //              bytes = item.DataFiles;

        //             return File(bytes, contentType, fileName);
        //         }

        //        return View();
        //      }
        public async Task<IActionResult> DownloadImage(int id)
        {
            var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync();
            if (file == null) return null;
            return File(file.DataFiles, file.Name   file.FileType);
        }
    }
}

CodePudding user response:

this code is working with me

public async Task<IActionResult> DownloadImage(string filename,int id)
    {
        if (filename == null)
            return Content("filename is not availble");
        var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync();

        var path = Path.Combine(Directory.GetCurrentDirectory(), filename);

        var memory = new MemoryStream();
       
        {
          
        }
        memory.Position = 0;
        return File(memory, GetContentType(path), Path.GetFileName(path));
    }
    private string GetContentType(string path)
    {
        var types = GetMimeTypes();
        var ext = Path.GetExtension(path).ToLowerInvariant();
        return types[ext];
    }

    private Dictionary<string, string> GetMimeTypes()
    {
        return new Dictionary<string, string>
        {
            {".txt", "text/plain"},
            {".pdf", "application/pdf"},
            {".doc", "application/vnd.ms-word"},
            {".docx", "application/vnd.ms-word"},
            {".xls", "application/vnd.ms-excel"},
            {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
            {".png", "image/png"},
            {".jpg", "image/jpeg"},
            {".jpeg", "image/jpeg"},
            {".gif", "image/gif"},
            {".csv", "text/csv"}
        };
    }

    public IActionResult Privacy()
    {
        return View();
    }

}

}

CodePudding user response:

Try returning the null within the OkObjectResul.

return Ok(null);
  • Related