如何在不打开浏览器的情况下下载两个不同的扩展名文件



我正在上传.doc/docx或.pdf文件,我希望在不打开浏览器的情况下下载。

if(file1 == null)
{
ModelState.AddModelError("CustomError", "Please Select Word or PDF file");
return View();
}
if(!(file1.ContentType == "application/doc" || file1.ContentType == "application/pdf" || file1.ContentType == "application/docx" ))
{
ModelState.AddModelError("CustomError", "Please Select .doc or .pdf file only");
return View();
}
public FileResult Download(string file1)
{
string filename = file1;
string Path1 = Server.MapPath("~/Files");
string FullPath = Path.Combine(Path1, file1);
byte[] fileBytes = System.IO.File.ReadAllBytes(FullPath);
return File(fileBytes, "application/pdf");
}

您可以使用File方法的重载将文档下载为文件,而不是在浏览器中打开它:

return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, filename);

使用通用的八位位组流MIME类型意味着浏览器不会试图解释文件类型来决定是将其显示还是将其作为文件下载。

最新更新