3

there.I have been coding in java SE from long time. I am absolute noob in ASP.NET Core MVC and REST API. 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.

I have been going through this stackoverflow question but i find it very cluttery Using .NET, how can you find the mime type of a file based on the file signature not the extension

Edit:

Now i have narrowed down the question to only download the PDF files and verify the PDF file.

Adding the coding effort i have done so far to solve this. Now i am stuck with the request and response creation for the rest 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 if the file is 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 GET request. Please suggest.

wandermonk
  • 6,856
  • 6
  • 43
  • 93
  • Downvoting a question should be very easy. But , for any newbie this is a very difficult question. – wandermonk Jun 19 '17 at 07:04
  • I upvoted you, but you should do some research on your own and then be more specific with your question, because right now it is very 'vast'. – pitersmx Jun 19 '17 at 07:10
  • This question is off-topic here for two reasons: you are asking us to recommend a tool. You are actually asking three or four questions in one. We can't answer all at once. – Patrick Hofman Jun 19 '17 at 07:14
  • 1
    First question: https://stackoverflow.com/questions/3938569/how-do-i-upload-a-file-with-metadata-using-a-rest-web-service Second question: You can only zip on your server, how would you zip on the client? Third question: By passing a hash with your download and later compare the hash provided with the locally created hash. – jAC Jun 19 '17 at 07:16
  • 1
    Hi and welcome to Stackoverflow. The problem with this question is it is asking too many things over a single question which is difficult for people to help you with. You have an assignment, you have to break it down into smaller parts. It seems complex to you because you are trying to grasp it all at once. This is a great place to ask for help but you have understand a bit of how you should proceed. Refer this- https://stackoverflow.com/help/mcve and https://stackoverflow.com/help/how-to-ask – Souvik Ghosh Jun 19 '17 at 07:17
  • @PatrickHofman I have narrowed down my question. Please let me know if this is still too broad. – wandermonk Jun 19 '17 at 11:09
  • @pitersmx please suggest. – wandermonk Jun 19 '17 at 11:10
  • @PatrickHofman please remove the off topic . I rephrased my question as per guidelines. – wandermonk Jun 20 '17 at 05:05

0 Answers0