2011-03-29 93 views
1

我想提示保存對話框到user.The文件taype是.wav。的動作如下所示提示要保存的文件-MVC

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:/本地主機:2694/DATA/MERGE/OUT/1/CCCC'

但它示出了下面

'http:/localhost:2694/DATA/MERGE/OUT/1/cccc' is not a valid virtual path. 
所示的錯誤

這是一個正確的方式來提示用戶保存文件對話框?

編輯

有時有availbale用戶沒有文件。所以我只想顯示一個警告,如果urlFilePath =「」。

如果沒有文件路徑可我怎麼能返回一個空resul.And使警報user..The事情我要的是下面hown

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 
     } 
     } 

回答

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

@ Darin.This is working。日Thnx。但是,如果我沒有文件返回,我怎麼能從行動返回(我想不會刷新引起的視圖) – 2011-03-29 06:41:04

+0

@Null指針,我不明白你的問題。你能否詳細說明一下? – 2011-03-29 06:44:34

+0

@Sarin,Thanx ..請看編輯的問題 – 2011-03-29 06:50:58