我想向用户提示保存对话框。文件taype是.wav。行动如下所示
public ActionResult MergeSelectedRecords(string mergeFileName, List<String> selectedRecords)
{
string urlFilePath=GetFilePath(); //get virtual path of file.
ControllerContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + "cccc");
string filePath = ControllerContext.HttpContext.Server.MapPath(urlFilePath);
return File(filePath, ".wav");
}
一个sampl文件路径是'http:/ localhost:2694 / DATA / MERGE / OUT / 1 / cccc'
但它显示如下所示的错误
'http:/localhost:2694/DATA/MERGE/OUT/1/cccc' is not a valid virtual path.
这是向用户提示保存文件对话框的正确方法吗?
修改
有时用户没有文件可用。所以我只想在urlFilePath =“”。
时显示警告如果没有可用的文件路径我怎么能返回一个空的结果。并向用户发出警报..我想要的东西在下面显示
public ActionResult MergeSelectedRecords(string mergeFileName, List<String> selectedRecords)
{
string urlFilePath=GetFilePath(); //get virtual path of file.
if(urlFilePath!="")
{
ControllerContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + "cccc");
string filePath = ControllerContext.HttpContext.Server.MapPath(urlFilePath);
return File(filePath, ".wav");
}
else
{
//what i return here? If it possible i only want to display an alert .But the page user viewing cannot refreshed
}
}
答案 0 :(得分:1)
您传递给urlFilePath
方法的MapPath
参数必须是以~/
开头的同一网站中的相对网址。例如:
public ActionResult MergeSelectedRecords(string mergeFileName, List<String> selectedRecords)
{
string urlFilePath = "~/Files/ccc.wav";
string filePath = Server.MapPath(urlFilePath);
return File(filePath, ".wav", "ccc");
}
如果网址不是您网站的一部分,则需要先下载该文件。例如:
public ActionResult MergeSelectedRecords(string mergeFileName, List<String> selectedRecords)
{
using (var client = new WebClient())
{
byte[] file = client.DownloadData("http://foo.com/ccc.wav");
return File(file, ".wav", "ccc");
}
}