Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda