在mvc中使用Action Result作为返回类型时,return文件不存在
return file not exist when use Action Result as returen type in mvc
我有一个动作链接
@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("alert('file not exist')"),把它给了我空白页的文本,我把"alert('file not exist')"对这个问题有什么帮助吗提前感谢
你可以这样做:
if (System.IO.File.Exists(FullPath))
{ //....
}
else { return Content("Some error message"); }
但我宁愿返回404,如果该文件不存在。
相关文章:
- mongodb中db.collection.find()的返回类型是什么
- 了解存储的MongoDB javascript函数中MongoDB命令的返回类型
- 没有返回类型的函数的Javascript单元测试
- 如何在打字稿中声明私有类成员的参数和返回类型
- 在 JavaScript 中返回一个 return
- 重新打开程序函数返回类型错误“无法读取 null 的属性'样式'”
- 为什么可以在 JavaScript 中添加参数类型和返回类型
- 我应该如何声明一个函数的返回类型,该函数返回一个没有数据的$q promise
- 为什么PowerShell函数的返回类型总是数组
- AngularJS工厂返回类型未定义
- javascript:确定函数的返回类型
- Javascript'在'循环内部的运算符返回类型错误
- 使用c++中的构造函数时,ffi的返回类型是什么?
- 显式设置函数的返回类型
- React无状态功能组件的流返回类型是什么?
- Node.js表示json的返回类型
- Typescript编译器为Reselect.createSelector()生成了错误的返回类型
- Javascript返回类型错误"无法获取属性'1'未定义或空引用的
- TS1057:异步函数或方法必须具有有效的可等待返回类型
- 在mvc中使用Action Result作为返回类型时,return文件不存在