I have a very basic understanding on the REST. I am working on a requirement which needs to download different types of files from the file path to the local folder.
If the user checks one or more files and then click download then it should be downloaded as a zip file to the local folder.
- The first point is how do i know if the incoming file is a .pdf,.docx,.xlsx or any other file type.
- Where should i zip (at client/server) the files and then download the file to the users local machine.
- How can i ensure that the whole file is downloaded successfully.
For a start, i am concentrating only on exposing PDF files using REST API and make them download on click.
I am using the below code to determine the files mime type. Please let me know if there is any better way of doing this. My controller on GET request needs to determine the file is PDF and then download the file to the local computer.
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; } } }