English 中文(简体)
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

问题回答

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签