'Prevent IDM from downloading automatically in web api

I have a web api method that returns an HttpResponseMessage containing a PDF file. The method looks something like this:

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(path, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;

When I call this api from client (which is written in angularJS), the Internet Download Manager automatically catches the PDF file and wants to download it. And because I have a security plan for my project, the IDM automatically requests username and password. Does anyone have an idea about how I'm supposed to programmatically stop IDM from catching the PDF file?

Update: Here's my angularJS code:

$http.post(url, { transactionId: txId }
            , {responseType: 'arraybuffer'})
            .success(function (response) {
                var reader = new FileReader();
                var file = new Blob([response.data], {type: 'application/pdf'});
                reader.onload = function (e) {
                    var printElem = angular.element('#printPdfLink');
                    printElem.attr('target', '_blank');
                    printElem.attr('href', reader.result);
                    printElem.attr('ng-click', '');
                };
                reader.readAsDataURL(file);
            })
            .error(function (error) {});


Solution 1:[1]

Change the mime type to application/octet-stream as a way to work around your problem. Make sure that the file name includes a proper file extension so that it can be recognized by the client system once downloaded.

Another issue is the attachment disposition of the content which typically forces it to save it as a file download. Change it to inline so that the client can consume it without IDM trying to download it as an attachment.

FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamContent content new StreamContent(stream);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
content.Headers.ContentDisposition.FileName = fileName;
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = content;
return response;

Solution 2:[2]

I have try to use HttpResponseMessage.

If I use ContentDisposition is inline then response break the file. If use attachment then IDM can detect it.

At the end of the day, I found Accept-Ranges header can make download without IDM but it not valid in HttpResponseMessage.

You can try out my code below to make download file without IDM:

[HttpGet]
[Route("~/download/{filename}")]
public void Download(string filename)
{
    // TODO lookup file path by {filename}
    // If you want to have "." in {filename} you need enable in webconfig
    string filePath = "<path>"; // your file path here
    byte[] fileBytes = File.ReadAllBytes(filePath);
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
    HttpContext.Current.Response.ContentType = "application/octet-stream";
    HttpContext.Current.Response.AddHeader("ContentDisposition", "attachment, filename=" + filename);
    HttpContext.Current.Response.BinaryWrite(fileBytes);
    HttpContext.Current.Response.End();
}

Note: filename parameter serve for download file name so you can config in webconfig if you want to have file extension (disabled by default).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Nguy?n Top