0

I need to download every file of type js and c#.

This is my api code:

[HttpPost]

public HttpResponseMessage GetFile(DownloadInput input)
{
    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
    var fileNm = input.FileName;
    string filePath = (@"C:\Uploads\" + input.ID + @"\" + fileNm);

    if (!File.Exists(filePath))
    {
        response.StatusCode = HttpStatusCode.NotFound;
        response.ReasonPhrase = string.Format("File not found: {0} .", fileNm);
        throw new HttpResponseException(response);
    }
    byte[] bytes = File.ReadAllBytes(filePath);
    response.Content = new ByteArrayContent(bytes);
    response.Content.Headers.ContentLength = bytes.LongLength;
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = fileNm;
    response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(filePath));
    return response;
}

I think ı am rong this part this code download every file but just work .txt type file, I think blob type is false but ı am new this subject ı am tried every code ,

This is my js code:

function FileDown(response, name) {
    let blob = new Blob([response], { type: "application/octet-stream" });
    let link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = name;
    link.click();
}
nalply
  • 26,770
  • 15
  • 78
  • 101
  • Just to verify, are you saying this will only download .TXT files and that is successful? What error are you getting for other types and on what line does the error come up? – Jimmy Smith Feb 04 '20 at 14:33

1 Answers1

0

You can try the following, I've used it successfully many times.

First you need to make sure the data you save is correct. Since you need the mimetype to successfully download files without complications.

Here is the C# (PS: You can make your Endpoint HttpGet, instead of HttpPost.)

[HttpGet]
[Route("YourController/{fileName}")]
public HttpResponseMessage Download(string fileName) //Parameter is yours
{
    string filePath = (@"C:\Uploads\" + input.ID + @"\" + fileName);

    if (!File.Exists(filePath))
    {
        response.StatusCode = HttpStatusCode.NotFound;
        response.ReasonPhrase = string.Format("File not found: {0} .", fileNm);
        throw new HttpResponseException(response);
    }
    byte[] bytes = File.ReadAllBytes(filePath);

    using (var ms = new MemoryStream(bytes))
    {
        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(ms.ToArray())
        };

        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = fileName //String value of the file name.
        };

        string mimeType = MimeMapping.GetMimeMapping(fileName); //I've found that this does not always work. Go here for a better answer: https://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension
        result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); //The mime type retrieved from the 

        return result;
    }
}

In javascript you can use: window.open('yourApiURL/YourController/FileName');

Marius
  • 1,420
  • 1
  • 11
  • 19