0
  1. 以下のコードは、[送信] ボタンを使用して Excel エクスポート コードをコントローラーの POST アクションに配置するとうまく機能します。
  2. 今、ポストバックして以下のようにコードを移動したくありませんが、コントローラーアクション「ExcelExport」が期待どおりに呼び出されましたが、レポートはダウンロードされませんが、レポートはダウンロードされませんか? ここで何が問題なのか教えてください。

モデル

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Decimal Price { get; set; }

    public Product(int id, string name, string description, decimal price)
    {
        Id = id;
        Name = name;
        Description = description;
        Price = price;
    }

    static readonly List<Product> AllProducts = new List<Product>
    {
        new Product(1, "Games Console", "Fun for all the family", 199.99m),
            new Product(2, "MP3 player", "Listen to your favourite tunes on the move", 99.99m),
            new Product(3, "Smart Phone", "Apps, apps, apps", 399.99m),
            new Product(4, "Digital Photo frame", "All your photos in one beautiful frame", 49.99m),
            new Product(5, "E-book reader", "Take your favourite books on the move with you", 149.99m),
            new Product(6, "DVD Box Set", "All of season one plus exclusive extras", 39.99m)
    };

    public static List<Product> GetAllProducts()
    {
        return AllProducts;
    }

コントローラ

public ActionResult Index()
    {
        return View();
    }


    public ActionResult ExcelExport()
    {
        var products = Product.GetAllProducts();

        var grid = new GridView();
        grid.DataSource = from p in products
                          select new
                          {
                              ProductName = p.Name,
                              SomeProductId = p.Id
                          };
        grid.DataBind();

        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");

        Response.ContentType = "application/excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        grid.RenderControl(htw);

        Response.Write(sw.ToString());

        Response.End();

        return Content("tr");
    }

意見

@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{


<div id="rptExport" style="display: none">
</div>

<input type="button" id="ExportExcelButton" value="Export To Excel" onclick="ExportExcel()" />
}

<script type="text/javascript">
//global cache false
$.ajaxSetup({ cache: false });

function ExportExcel() {
    //block the UI until the request is rendered
    $.blockUI({ message: '<h3><b><img src="@Url.Content("~/content/images/loading.gif")" />Please wait while Processing</b></h3>' });

    $('#rptExport').load('@Url.Action("ExcelExport", "Home")', function (data) {
        var urlFile = "";
        if (data != '') {
            debugger;
            //unblock the UI     
            $.unblockUI();
        }
    });
}

4

1 に答える 1

1

AJAX を使用してファイルをダウンロードすることはできません。
通常のページ リクエストを使用する必要があります。

于 2012-08-28T15:01:21.610 に答える