提示保存文件-MVC

发布于 2024-10-27 17:41:55 字数 1499 浏览 0 评论 0原文

我想向用户提示一个保存对话框。文件类型是 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名的好友 2024-11-03 17:41:55

您传递给 MapPath 方法的 urlFilePath 参数必须是同一站点内以 ~/ 开头的相对 URL。示例:

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");
    }
}

The urlFilePath argument that you are passing to the MapPath method must be a relative url within the same site starting with ~/. Example:

public ActionResult MergeSelectedRecords(string mergeFileName, List<String> selectedRecords)
{
    string urlFilePath = "~/Files/ccc.wav";
    string filePath = Server.MapPath(urlFilePath);
    return File(filePath, ".wav", "ccc");
}

If the url is not part of your site you will need to download the file first. Example:

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文