Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {});
like image 217
Sara G Avatar asked Mar 29 '17 07:03

Sara G


1 Answers

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.

var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
var content new StreamContent(stream);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
content.Headers.ContentDisposition.FileName = fileName;
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = content;
return response;
like image 79
Nkosi Avatar answered Sep 25 '22 03:09

Nkosi