REST API: Uploading Files from Front End to Backend Server

there. I have been coding in java SE for a long time. I am absolutely a noob at ASP.NET Core MVC and REST API. I have a basic understanding of REST. I am working on a requirement that requires loading different types of files from a file path to a local folder.

If the user checks one or more files and then downloads, it should be downloaded as a zip file in a local folder.

I am reviewing this question with stackoverflow but I find it very messy Using .NET how can you find the mime type of a file based on the filename and not the extension

Edit:

Now I have narrowed down the question to only download PDF files and validate the PDF file.

Adding the coding effort I've done so far to solve this. Now I am stuck with making request and response for the rest of the api to download the file.

Controller class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;


namespace FileDownloaderService.Controllers
{
    public class FileDownloadController : Controller
    {
        [HttpGet]
    public HttpResponseMessage Get() {
        string filename = "ASPNETCore" + ".pdf";
        string path = @"C:\path_to_file\"+ filename;

        if (System.IO.File.Exists(path)) {
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            var stream = new FileStream(path, FileMode.Open,FileAccess.Read);
            stream.Position = 0;
            response.Content = new StreamContent(stream);
            response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = filename };
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
            response.Content.Headers.ContentDisposition.FileName = filename;
            return response;
        }
        else
        {
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Gone);
            return response;
        }
    }

    }
}

      

Class to find a file of type PDF:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;

namespace FileDownloaderService.Utilities
{
    public class FileMimeType
    {
        //Byte sequence for PDF extensions.
        private static readonly byte[] PDF = { 37, 80, 68, 70, 45, 49, 46 };
        private static readonly string DEFAULT = "application/octet-stream";

        //Method to check the file mime type.
        public static string GetMimeType(byte[] file,string filename) {

            string mime = DEFAULT;

            if (string.IsNullOrWhiteSpace(filename)) {
                return mime;
            }
            string extension = Path.GetExtension(filename) == null ? string.Empty : Path.GetExtension(filename).ToUpper();

            if (file.Take(7).SequenceEqual(PDF)) {
                mime = "application/pdf";
            }

            return mime;
        }
    }
}

      

I am unable to download the file on a GET request. Please suggest.

+3


source to share





All Articles