Chrome 忽略从 WebAPI 控制器发送的 HttpResponse 的内容处置标头中建议的扩展名



我正在尝试编写一个 WebAPI 控制器,该控制器动态生成 Windows 快捷方式文件 (.lnk) 并将其发送回客户端,但即使我在构建响应时在内容处置标头中指定".lnk"作为扩展名,Chrome 仍然在"另存为"对话框中将其更改为".download"。我已经尝试使用应用程序/x-ms快捷方式和应用程序/octet-stream作为内容类型,但它在两种情况下具有相同的行为。

有谁知道如何避免这种情况并强制它在对话框中建议"链接.lnk"?

注意 1:我使用的是 Chrome 47

注2:在IE 11中工作正常

var response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(linkPath, FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "link.lnk" } ;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-ms-shortcut");
response.Content.Headers.ContentLength = stream.Length;
return response;

我上面评论的完整版本...

如果你在一个名为 EnsureSafeExtension 的方法中查看铬源代码,我们会看到代码:

if (IsShellIntegratedExtension(extension))
  extension = kDefaultExtension; //<--"download"

其中IsShellIntegratedExtension对于lnk扩展返回 true:

if ((extension_lower == FILE_PATH_LITERAL("local")) ||
    (extension_lower == FILE_PATH_LITERAL("lnk")))
  return true;

代码中的注释说:

// Right-clicking on shortcuts can be magical.

因此,由于Chrome开发人员认为漏洞潜伏在.lnk文件的右键单击行为中,您会看到预期的行为。

最新更新