gpt4 book ai didi

javascript - 在 mvc 中使用 Action Result 作为返回类型时返回文件不存在

转载 作者:行者123 更新时间:2023-11-29 15:33:49 24 4
gpt4 key购买 nike

我有操作链接

    @Html.ActionLink(@Resources.Resource_ar.Download, "DownloadFile", new { Communicationid = item.Communicationid }, new { @class = "btn btn-success btn_Download" })

调用下载文件的 Action

   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。

关于javascript - 在 mvc 中使用 Action Result 作为返回类型时返回文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31695888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com