我有行动链接
@Html.ActionLink(@Resources.Resource_ar.Download, "DownloadFile", new { Communicationid = item.Communicationid }, new { @class = "btn btn-success btn_Download" })
调用下载文件的操作
public ActionResult DownloadFile(string Communicationid)
{
string pathString = Settings.getSettingValue("FolderForSaveCommunicationsAttachments");
//string FullPath = Path.Combine(Server.MapPath("~/Content/UploadedAttachment/"), FileName);
string FullPath = Path.Combine(pathString, Communicationid);
Communications ObjCommunication = new Communications(int.Parse(Communicationid));
string FileName = ObjCommunication.s_FileName;
//return File(FullPath, "text/docx");
if (System.IO.File.Exists(FullPath))
{
string contentType = string.Empty;
if (FileName.Contains(".pdf"))
{
contentType = "application/pdf";
}
else if (FileName.Contains(".docx"))
{
contentType = "application/docx";
}
else if (FileName.Contains(".doc"))
{
contentType = "application/doc";
}
else if (FileName.Contains(".jpeg"))
{
contentType = "image/jpeg";
}
else if (FileName.Contains(".jpg"))
{
contentType = "image/jpg";
}
else if (FileName.Contains(".png"))
{
contentType = "image/png";
}
else if (FileName.Contains(".bmp"))
{
contentType = "image/bmp";
}
else if (FileName.Contains(".xlsx"))
{
contentType = "application/xlsx";
}
else if (FileName.Contains(".Exl"))
{
contentType = "application/Exl";
}
else if (FileName.Contains(".txt"))
{
contentType = "application/txt";
}
return File(FullPath, contentType, FileName);
}
else
{
return;
}
}
问题是,当文件存在时,它返回文件并正确下载但是当文件不存在时我想向用户显示警告告诉他文件不存在我应该返回什么 我尝试返回javascript(“警告('文件不存在')”)把它给了我空白页面,文字我把“警告('文件不存在')” 任何有关此问题的帮助 提前谢谢
答案 0 :(得分:1)
您可以这样做:
if (System.IO.File.Exists(FullPath))
{ //....
}
else { return Content("Some error message"); }
但如果该文件不在那里,我宁愿返回404.