如何从一个文件夹下载pptx文件到客户端系统-MVC
How to download pptx file from one folder to client system - MVC
我在一个位置有 .pptx 文件,我需要在用户想要保存的客户端系统或默认浏览器下载位置下载这些文件。
控制器代码
var fileName = "textFile20210323.pptx";
var filePath = @"\Depts\IT\TestFolder\";
var fileNamewithPath = $"{filePath}{fileName}";
Response.ClearHeaders();
Response.Clear();
Response.ContentType = "application/x-mspowerpoint";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNamewithPath);
Response.WriteFile(fileNamewithPath);
Response.Flush();
return Json(new { success = "success" }, JsonRequestBehavior.AllowGet);
脚本
function DownloadFile(args) {
$.ajax({
type: "POST",
url: "../Home/DownloadFile",
data: { "json": JSON.stringify(args) },
dataType: "json",
beforeSend: function () {
},
success: function (data) {
alert("Success");
}
});
}
任何其他方法都是可以接受的。
如前所述,简单的方法是根本不使用 Ajax。下面给用户一个 link 将文件下载到他们的本地计算机。
查看:
<a href="@Url.Action("GetFile", new { controller = "Home", path = @"\Depts\IT\TestFolder\textFile20210323.pptx" })">Download File</a>
控制器:
using System.IO;
public FilePathResult GetFile(string path)
{
string fileName = Path.GetFileName(path);
return File(path, "application/octet-stream", fileName);
}
Controller.cs
public FileResult DownloadFile(string FilePath, string FileName)
{
if (!string.IsNullOrEmpty(FilePath) && !string.IsNullOrEmpty(FileName))
{
return File(FilePath, "application/vnd.ms-powerpoint", FileName);
}
else
{
return null;
}
}
View.cshtml
<a onClick="DownloadFile(this)">Download</a>
Script.js
function DownloadFile(args) {
window.location.href = "../Home/DownloadFile?FilePath=" + args.FilePath + "&FileName=" + args.Name;
}
我在一个位置有 .pptx 文件,我需要在用户想要保存的客户端系统或默认浏览器下载位置下载这些文件。
控制器代码
var fileName = "textFile20210323.pptx";
var filePath = @"\Depts\IT\TestFolder\";
var fileNamewithPath = $"{filePath}{fileName}";
Response.ClearHeaders();
Response.Clear();
Response.ContentType = "application/x-mspowerpoint";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNamewithPath);
Response.WriteFile(fileNamewithPath);
Response.Flush();
return Json(new { success = "success" }, JsonRequestBehavior.AllowGet);
脚本
function DownloadFile(args) {
$.ajax({
type: "POST",
url: "../Home/DownloadFile",
data: { "json": JSON.stringify(args) },
dataType: "json",
beforeSend: function () {
},
success: function (data) {
alert("Success");
}
});
}
任何其他方法都是可以接受的。
如前所述,简单的方法是根本不使用 Ajax。下面给用户一个 link 将文件下载到他们的本地计算机。
查看:
<a href="@Url.Action("GetFile", new { controller = "Home", path = @"\Depts\IT\TestFolder\textFile20210323.pptx" })">Download File</a>
控制器:
using System.IO;
public FilePathResult GetFile(string path)
{
string fileName = Path.GetFileName(path);
return File(path, "application/octet-stream", fileName);
}
Controller.cs
public FileResult DownloadFile(string FilePath, string FileName)
{
if (!string.IsNullOrEmpty(FilePath) && !string.IsNullOrEmpty(FileName))
{
return File(FilePath, "application/vnd.ms-powerpoint", FileName);
}
else
{
return null;
}
}
View.cshtml
<a onClick="DownloadFile(this)">Download</a>
Script.js
function DownloadFile(args) {
window.location.href = "../Home/DownloadFile?FilePath=" + args.FilePath + "&FileName=" + args.Name;
}