Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
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) {});
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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;
over 4 years ago · Santiago Trujillo Report

0

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).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!