无法从返回类型为“Microsoft.AspNetCore.Mvc.IActionResult”的操作方法返回null

liwlm1x9  于 2022-12-20  发布在  .NET
关注(0)|答案(2)|浏览(224)

亲爱的,我正在使用asp.net核心3.1的问题,当我下载它给我这个错误(无法返回空从一个操作方法与返回类型'Microsoft.AspNetCore.Mvc.IActionResult)
这个模型

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

这个控制器

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);
        }
    }
}
6tdlim6h

6tdlim6h1#

这个代码对我有用

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();
    }

}

}

flvlnr44

flvlnr442#

尝试在OkObjectResul中返回空值。

return Ok(null);

相关问题