提示保存文件-MVC
我想向用户提示一个保存对话框。文件类型是 .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");
}
样本文件路径为“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
}
}
I want to prompt a save dialog to user.The file taype is .wav. The action is shown below
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");
}
A sampl file path is 'http:/localhost:2694/DATA/MERGE/OUT/1/cccc'
But it showing an error shown below
'http:/localhost:2694/DATA/MERGE/OUT/1/cccc' is not a valid virtual path.
Is this is a right way a to prompt a save file dialog to user?
EDIT
Sometimes there is no file availbale for user. So i only want to show an alert if the urlFilePath="".
IF no filepath is available How can i return an empty resul.And make an alert to user..The things i want is hown below
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您传递给
MapPath
方法的urlFilePath
参数必须是同一站点内以~/
开头的相对 URL。示例:如果该网址不属于您网站的一部分,您需要先下载该文件。例子:
The
urlFilePath
argument that you are passing to theMapPath
method must be a relative url within the same site starting with~/
. Example:If the url is not part of your site you will need to download the file first. Example: