用mvc下载一个文件
Download a file with mvc
我需要允许我网站上的用户下载文件(xml 文件)
我试过了
public FileResult DownloadFile(string fileid)
{
if (!string.IsNullOrEmpty(fileid))
{
byte[] fileBytes = Encoding.ASCII.GetBytes(FileData);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet,FileName + ".bccx");
}
return null;
}
ajax:
function downloadFile(id) {
$.ajax({
url: sitePath + "Controller/DownloadFile?fileid=" + id,
type: 'post',
asysnc: false
})
.done(function () {
});
}
但没有下载任何内容
这是我完成下载的一种方式,希望对你有帮助。
$.ajax({
url: sitePath + "Controller/DownloadFile?fileid=" + id,
type: 'GET',
success: function (filename) { //I return the filename in from the controller
frame = document.createElement("iframe");
frame.id = "iframe";
frame.style.display = 'none';
frame.src = '/FileDirectory?fileName=' + filename; //the path to the file
document.body.appendChild(frame);
},
cache: false,
contentType: false,
processData: false
});
是否必须使用 ajax 来完成?也许你可以用文件生成地址打开另一个 window 并让浏览器完成这项工作:
function downloadFile(id) {
window.open(sitePath + "Controller/DownloadFile?fileid=" + id, '_blank');
}
您不能使用 ajax post 下载文件。
它不能将文件直接保存到用户的机器中。 ajax post 会得到原始格式的响应,但它不会是一个文件。
只需使用
function downloadFile(id) {
window.location = "/Controller/DownloadFile?fileid=" + id;
}
很简单
制作link
<a href="/Home/preview?id=Chrysanthemum.jpg" > Download File</a>
和您的控制器
public ActionResult preview(string id)
{
string Filename = Path.GetFileName(@id);
string location = id;
try
{
if (System.IO.File.Exists(location))
{
FileStream F = new FileStream(@location, FileMode.Open, FileAccess.Read, FileShare.Read);
return File(F, "application/octet-stream", Filename);
}
}
catch (IOException ex)
{
}
return View();
}
我需要允许我网站上的用户下载文件(xml 文件)
我试过了
public FileResult DownloadFile(string fileid)
{
if (!string.IsNullOrEmpty(fileid))
{
byte[] fileBytes = Encoding.ASCII.GetBytes(FileData);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet,FileName + ".bccx");
}
return null;
}
ajax:
function downloadFile(id) {
$.ajax({
url: sitePath + "Controller/DownloadFile?fileid=" + id,
type: 'post',
asysnc: false
})
.done(function () {
});
}
但没有下载任何内容
这是我完成下载的一种方式,希望对你有帮助。
$.ajax({
url: sitePath + "Controller/DownloadFile?fileid=" + id,
type: 'GET',
success: function (filename) { //I return the filename in from the controller
frame = document.createElement("iframe");
frame.id = "iframe";
frame.style.display = 'none';
frame.src = '/FileDirectory?fileName=' + filename; //the path to the file
document.body.appendChild(frame);
},
cache: false,
contentType: false,
processData: false
});
是否必须使用 ajax 来完成?也许你可以用文件生成地址打开另一个 window 并让浏览器完成这项工作:
function downloadFile(id) {
window.open(sitePath + "Controller/DownloadFile?fileid=" + id, '_blank');
}
您不能使用 ajax post 下载文件。 它不能将文件直接保存到用户的机器中。 ajax post 会得到原始格式的响应,但它不会是一个文件。
只需使用
function downloadFile(id) {
window.location = "/Controller/DownloadFile?fileid=" + id;
}
很简单
制作link
<a href="/Home/preview?id=Chrysanthemum.jpg" > Download File</a>
和您的控制器
public ActionResult preview(string id)
{
string Filename = Path.GetFileName(@id);
string location = id;
try
{
if (System.IO.File.Exists(location))
{
FileStream F = new FileStream(@location, FileMode.Open, FileAccess.Read, FileShare.Read);
return File(F, "application/octet-stream", Filename);
}
}
catch (IOException ex)
{
}
return View();
}