1

C#-MVC3 を使用しています。「エクスポート」ページがあります。DB からさまざまなテーブルをエクスポートする関数がいくつかあります。すべての関数は、テーブルから CSV ファイルを作成し、FileContentResult ファイルをユーザーに返します。

ここで、すべてのファイルを一度にダウンロードするために、「すべてエクスポート」のボタンを作成したいと考えています。ZipFile を使用しようとしましたが、「FileContentResult」ファイルではなく、サーバーに保存されたファイル名とパスのみを取得します。

そのため、「FileContentResult」ファイルを一時的にサーバーに保存し、圧縮して削除したかったのですが、「FileContentResult」ファイルを保存する方法が見つかりません。

あなたが私を助けることができるか、私に別のアイデアを与えることができれば、私は聞いてうれしい.

4

1 に答える 1

1

私の解決策:

    public ZipFile DownloadAllToZip()
    {
        string path = "c:\\TempCSV";
        try
        {
            if (Directory.Exists(path))
            {
                EmptyFolder(path);
            }
            else
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }
            List<FileContentResult> filesToExport = GetAllCSVs();

            foreach (var file in filesToExport)
            {
                try
                {
                    using (FileStream stream = new FileStream(path + "\\" + file.FileDownloadName, FileMode.CreateNew))
                    {
                        using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
                        {
                            byte[] buffer = file.FileContents;
                            stream.Write(buffer, 0, buffer.Length);
                            writer.Close();
                        }
                    }
                }
                catch { }
            }
        }
        catch{ }

        Response.Clear();
        Response.BufferOutput = false;
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "attachment; filename=MaterialAssetTracker.zip");
        ZipFile zip= new ZipFile();
        using (zip)
        {
            zip.CompressionLevel = CompressionLevel.None;
            zip.AddSelectedFiles("*.csv", path + "\\", "", false);
            zip.Save(Response.OutputStream);
        }
        Response.Close();
        EmptyFolder(path);
        return zip;
    }
于 2012-06-11T06:41:53.153 に答える