Blazor API - 在新选项卡中显示(pdf、xlsx、ppt、视频)而不是下载提示
Blazor API - Display (pdf,xlsx,ppt,video) in new tab instead of download prompt
API-服务器端
public FileResult DownloadFile(int ProductID, string FileName, int UserID)
{
var folderName = Path.Combine("Files", root);
string FilePath = '';
FilePath = Path.Combine(Directory.GetCurrentDirectory(), folderName);
var fullPath = Path.Combine(FilePath, FileName);
byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
Response.Headers.Add("Content-Disposition", "inline; filename=" + FileName);
return File(fileBytes,System.Net.Mime.MediaTypeNames.Application.Octet,FileName);
}
结果总是下载文件不显示
我是否必须在响应中添加一些特殊内容才能使浏览器自动打开文件?
非常感谢任何帮助!谢谢!
这取决于您的客户端浏览器设置。
如果您想“尝试”打开文件而不下载它,用户的浏览器应该能够理解文件格式。
如果是 PDF,您需要指定 "Content-type: application/pdf"
而不是源代码中使用的 Octet
。
MIME 类型有一个助手 class 具有这些值:
namespace System.Net.Mime
{
public static class MediaTypeNames
{
public static class Text
{
public const string Plain = "text/plain";
public const string Html = "text/html";
public const string Xml = "text/xml";
public const string RichText = "text/richtext";
}
public static class Application
{
public const string Soap = "application/soap+xml";
public const string Octet = "application/octet-stream";
public const string Rtf = "application/rtf";
public const string Pdf = "application/pdf";
public const string Zip = "application/zip";
public const string Json = "application/json";
public const string Xml = "application/xml";
}
public static class Image
{
public const string Gif = "image/gif";
public const string Tiff = "image/tiff";
public const string Jpeg = "image/jpeg";
}
}
}
所以在你的情况下你可以使用 System.Net.Mime.MediaTypeNames.Application.Pdf
.
但是,正如我所写,这取决于浏览器中安装和集成的计算机软件。
API-服务器端
public FileResult DownloadFile(int ProductID, string FileName, int UserID)
{
var folderName = Path.Combine("Files", root);
string FilePath = '';
FilePath = Path.Combine(Directory.GetCurrentDirectory(), folderName);
var fullPath = Path.Combine(FilePath, FileName);
byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
Response.Headers.Add("Content-Disposition", "inline; filename=" + FileName);
return File(fileBytes,System.Net.Mime.MediaTypeNames.Application.Octet,FileName);
}
结果总是下载文件不显示
我是否必须在响应中添加一些特殊内容才能使浏览器自动打开文件?
非常感谢任何帮助!谢谢!
这取决于您的客户端浏览器设置。
如果您想“尝试”打开文件而不下载它,用户的浏览器应该能够理解文件格式。
如果是 PDF,您需要指定 "Content-type: application/pdf"
而不是源代码中使用的 Octet
。
MIME 类型有一个助手 class 具有这些值:
namespace System.Net.Mime
{
public static class MediaTypeNames
{
public static class Text
{
public const string Plain = "text/plain";
public const string Html = "text/html";
public const string Xml = "text/xml";
public const string RichText = "text/richtext";
}
public static class Application
{
public const string Soap = "application/soap+xml";
public const string Octet = "application/octet-stream";
public const string Rtf = "application/rtf";
public const string Pdf = "application/pdf";
public const string Zip = "application/zip";
public const string Json = "application/json";
public const string Xml = "application/xml";
}
public static class Image
{
public const string Gif = "image/gif";
public const string Tiff = "image/tiff";
public const string Jpeg = "image/jpeg";
}
}
}
所以在你的情况下你可以使用 System.Net.Mime.MediaTypeNames.Application.Pdf
.
但是,正如我所写,这取决于浏览器中安装和集成的计算机软件。