【问题标题】:Prevent IDM from downloading automatically in web api防止 IDM 在 web api 中自动下载
【发布时间】:2017-08-22 14:08:18
【问题描述】:

我有一个 web api 方法,它返回一个包含 PDF 文件的 HttpResponseMessage。该方法如下所示:

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;

当我从客户端(用 angularJS 编写)调用这个 api 时,Internet 下载管理器会自动捕获 PDF 文件并想要下载它。由于我为我的项目制定了安全计划,IDM 会自动请求用户名和密码。 有谁知道我应该如何以编程方式阻止 IDM 捕获 PDF 文件?

更新:这是我的 angularJS 代码:

$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) {});

【问题讨论】:

  • 如果@Nkosi 的回答对您的问题不起作用,我认为您应该将您的Web api 地址放入IDM 配置中的"Don't start downloading automatically from the following sites:"...addresses(选项-文件类型)
  • 你能发布你的 Angular 代码来调用 api 吗?
  • @Kalyan 更新了问题
  • 你能把mcve 放在 plunker 或其他地方吗...我想知道代码是否真的触发了下载?

标签: c# angularjs pdf asp.net-web-api


【解决方案1】:

我尝试使用HttpResponseMessage

如果我使用 ContentDispositioninline 然后响应破坏文件。如果使用attachment,则IDM可以检测到。

最后,我发现Accept-Ranges 标头可以在没有 IDM 的情况下进行下载,但它在 HttpResponseMessage 中无效。

您可以在下面尝试我的代码来制作没有 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();
}

注意:filename 参数用于下载文件名,如果你想有文件扩展名,你可以在webconfig 中配置(默认禁用)。

【讨论】:

    【解决方案2】:

    将 mime 类型更改为 application/octet-stream 以解决您的问题。确保文件名包含正确的文件扩展名,以便客户端系统在下载后能够识别它。

    另一个问题是内容的attachment 处置通常会强制将其保存为文件下载。将其更改为inline,这样客户端就可以使用它,而无需 IDM 尝试将其作为附件下载。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-10
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多