我有这个功能:
public ActionResult Download(string FileName)
{
if (FileName != null)
{
string filepath = AppDomain.CurrentDomain.BaseDirectory + "/uploads/" + FileName;
byte[] filedata = System.IO.File.ReadAllBytes(filepath);
string contentType = MimeMapping.GetMimeMapping(filepath);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = FileName,
Inline = true,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(filedata, contentType);
}
return RedirectToAction("Index");
}
在本地服务器上工作的相同代码但在实时服务器中没有,任何人都可以告诉我原因吗?
答案 0 :(得分:0)
这可能不是你的答案,但你可以用一行写出整件事
使用return File(path, System.Net.Mime.MediaTypeNames.Application.Octet, name);
文件将被下载...(我在mvc4中使用它,它也在现场工作..)
例如,
var audio_path = "/" + path.Substring(path.IndexOf("Content"));
String audio_name = path.Substring(path.LastIndexOf("/") + 1);
return File(audio_path, System.Net.Mime.MediaTypeNames.Application.Octet, audio_name);
答案 1 :(得分:-1)
您可以使用以下代码下载文件:
#region Download File ==>
public ActionResult downloadfile(string Filename, string MIMEType)
{
try
{
string file_name = "/Files/EvidenceUploads/" + Filename;
string contentType = "";
//Get the physical path to the file.
string FilePath = Server.MapPath(file_name);
string fileExt = Path.GetExtension(file_name);
contentType = MIMEType;
//Set the appropriate ContentType.
Response.ContentType = contentType;
Response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(file_name)).Name);
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
return View(FilePath);
}
catch
{
Response.End();
return View();
//To Do
}
}
#endregion