0

リスト ビューがあり、すべてのデータを txt ファイルとしてエクスポートしたいと考えています。要件として、[エクスポート] ボタンをクリックして 3 つの txt ファイルを作成する必要があります。これらのファイルを生成し、zip ファイルとしてダウンロードするコントローラー アクションがあります。[エクスポート] ボタンをクリックすると、アクション「ExportFiles」がトリガーされます。同時に、ビューを更新したいので、アクション「リスト」にリダイレクトしたいと思います。

しかし、問題は、両方のタスクを同時に実行できないことです。どうすればそれができますか?

これは私のコードです。

    public virtual ActionResult List()
    {
        // Code : showing my list
        return view();
    }

    public virtual ActionResult ExportFiles()
    {
        // Code : Generating files
        return new ZipResult(filePath, fileName + ".zip");
        // HERE I WANT TO REFRESH MY VIEW
    }

    public class ZipResult : ActionResult
    {
        private readonly string _filePath;
        public string Filename { get; set; }

        public ZipResult(string filePath, string fileName)
        {
            _filePath = filePath;
            Filename = fileName;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var response = context.HttpContext.Response;
            response.ContentType = "application/gzip";
            using (var zip = new ZipFile())
            {
                zip.AddFile(_filePath);
                zip.Save(response.OutputStream);
                var cd = new ContentDisposition
                {
                    FileName = Filename,
                    Inline = false
                };
                response.Headers.Add("Content-Disposition", cd.ToString());
            }
        }

    }
4

2 に答える 2

0

むしろ、1 回の応答で更新とファイルのダウンロードの両方を行うことは不可能です。試してみると、「HTTP ヘッダーが送信された後にリダイレクトできません」のような例外が発生します。

戦略を少し調整する必要があります。fe:

  • [エクスポート] ボタンをクリックした後、JS/jQuery によって特定のタイムアウト後にページを手動で更新します。
  • ダウンロード URL にリダイレクトする追加のメタリフレッシュ タグを含むページを返します。 <meta http-equiv="refresh" content="5,url=DownloadFile.aspx" />

それが役に立てば幸い。

于 2013-12-31T19:57:17.713 に答える
0

Since your export will return a file, it sounds like you simply need to refresh the view that you're starting from (your list). If that is the case, then what I do is have the page refresh a few seconds after a download button is selected (i do this so the view will show updated download counts and date).

In the View (your list) I typically add an onclick event to the download button(s) that run a script similar to below. Everything else stays the same.

function ReloadPage()
{
    setTimeout(function () {
        window.location.reload(1);
    }, 5000);
}
于 2015-06-01T20:14:51.880 に答える