Search code examples
c#databasedownloadasp.net-core-mvcupload

Upload in the database and download it in ASP.NET Core v5


I want the file that I uploaded in the documentUploadProvinceLevels database table to be able to be download. I implemented the file upload part, but I don't know what to do for downloading. Please help me.

Upload code:

public bool UploadAvatarProvinceDB(IFormFile file, RegisterViewModel document)
{
        if (file != null)
        {
            if (file.Length > 0)
            {
                int department = 1;
                document.uploadDate = DateTime.Now;
                document.fileName = "no-document";
                document.description = "ثبت تصویر پرسنل";
                var docName = Path.GetFileName(file.FileName);
                var fileExtension = Path.GetExtension(docName);
                var newFileName = String.Concat(document.nationalCode, fileExtension);

                var objfiles = new DocumentUploadProvinceLevel()
                {
                    id = 0,
                    fileName = newFileName,
                    uploadDate = DateTime.Now,
                    title = "تصویر پرسنل",
                    fileFormat = fileExtension,
                    userId = document.nationalCode,
                    description = document.description,
                    department = department,
                    IsDelete=false
                };

                using (var target = new MemoryStream())
                {
                    file.CopyTo(target);
                    objfiles.dataBytes = target.ToArray();
                }
                _context.documentUploadProvinceLevels.Add(objfiles);
                _context.SaveChanges();

                return true;
            }
        }

        return false;
}

documentUploadProvinceLevels table: enter image description here


Solution

  • You can add Download method like:

            public IActionResult Index()
            {
                var result = _context.documentUploadProvinceLevels.ToList();
                return View(result);
            }
    
            public IActionResult Download(int id)
            {
                byte[] bytes;
                string fileName, contentType;
    
                var item = _context.documentUploadProvinceLevels.FirstOrDefault(c => c.id == id);
    
                if (item != null)
                {
                    fileName = item.fileName;
                    contentType = item.fileFormat;
                    bytes = item.dataBytes;
    
                    return File(bytes, contentType, fileName);
                }
    
                return Ok("Can't find the File");
            }
    

    And the Index view like:

    @model List<DocumentUploadProvinceLevel>
     
    
    <ul>
        @foreach (var item in Model)
        {
            <li>
                <a asp-action="Download"
                   asp-route-id="@item.id">
                    @item.fileName
                </a>
            </li>
        }
    </ul>
      
    

    Besides, we need to use HTTP Content-type header , by applying the Content-type header information tells the browser, the type of response it is getting from the server.

    We need fileFormat like: image/png, text/plain ....

    Change your var fileExtension = Path.GetExtension(docName); into:

     var fileExtension = file.ContentType.ToLower();   
    

    result:

    enter image description here