2016-03-22 32 views
0

下面給出的代碼對於我來說非常適合,當我在asp網絡中使用具有重定向和前端的純asp.net(aspx)應用程序時。 現在我的情況是,我有mvc和asp.net(aspx頁面) 混合模式的應用程序,所以我只是使用相同的頁面(aspx)下載文件,通過從mvc控制器方法調用它,這是行不通的。 我寫了ajax調用來調用控制器中的一個void方法,該方法將重定向到aspx頁面,在該頁面中寫入代碼,其餘情況如上所述。在aspx和MVC混合模式下的文件下載案例

**string path = HttpContext.Current.Server.MapPath("~/Reporting/OnePager.xlsm"); 
       string id = Request.Params["id"]; 
       HttpResponse response = this.Response; 
       response.Buffer = true; 
       response.Clear(); 
       response.ContentType = "application/vnd.ms-excel"; 
       response.AddHeader("content-disposition", "attachment; filename=" + id + "-One-Pager.xlsm"); 
       response.WriteFile(path); 
       response.Flush(); 
       response.End();** 

這裏是Ajax調用

if (arrselected.length > 0) { 
       $.ajax({ 
        type: 'POST', 
        url: '@Url.Action("ExportProjectOnePager", "controller")', 
        dataType: 'json', 
        contentType: "application/json; charset=utf-8", 
        data: JSON.stringify({ ID: arrselected[0]}), 
        success: function (output) { 
         if (output.notValid.length != arrselected.length) { 
          alert("success"); 
         } 

         if (output.notValid != "") { 
          alert("You do not have permission to see this IDs :" + output.notValid); 
         } 
        } 
       }); 
      } 

這裏是無效的控制器方法

public void ExportProjectOnePager(string ID) 
     { 
      string _Path = @"~/Reporting/ProjectOnePager.aspx?id=" + ID; 
      Response.Redirect(_Path); 
     } 
+0

你能告訴你的AJAX調用?此外,如果你的方法是無效的,你如何重定向到另一個視圖或頁面? –

+0

感謝您的快速響應和興趣,下面給出的是我的ajax調用mvc視圖.. –

+0

我已更新問題 –

回答

0

你可以做同樣的控制方法,但是你必須改變它返回FileResult或FileStreamResult,並將代碼複製到那裏。

你也可以改變控制方法返回RedirectResult和代碼使用內返回重定向(「aspx頁面」),而不是Response.Redirect的,返回類型爲void