1

渡されたドキュメント ID を取得する Web Api コントローラー メソッドがあり、要求された ID のドキュメント ファイルを個別に返す必要があります。この機能を実現するために、次のリンクから受け入れられた回答を試しましたが、機能していません。どこで間違えたのかわからない。

単一の WebApi メソッドから複数のバイナリ ファイルを提供する最良の方法は何ですか?

私のWeb APIメソッド、

   public async Task<HttpResponseMessage> DownloadMultiDocumentAsync( 
             IClaimedUser user, string documentId)
    {
        List<long> docIds = documentId.Split(',').Select(long.Parse).ToList();
        List<Document> documentList = coreDataContext.Documents.Where(d => docIds.Contains(d.DocumentId) && d.IsActive).ToList();

        var content = new MultipartContent();
        CloudBlockBlob blob = null;

        var container = GetBlobClient(tenantInfo);
        var directory = container.GetDirectoryReference(
            string.Format(DirectoryNameConfigValue, tenantInfo.TenantId.ToString(), documentList[0].ProjectId));

        for (int docId = 0; docId < documentList.Count; docId++)
        {
            blob = directory.GetBlockBlobReference(DocumentNameConfigValue + documentList[docId].DocumentId);
            if (!blob.Exists()) continue;

            MemoryStream memStream = new MemoryStream();
            await blob.DownloadToStreamAsync(memStream);
            memStream.Seek(0, SeekOrigin.Begin);
            var streamContent = new StreamContent(memStream);
            content.Add(streamContent);

        }            
        HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
        httpResponseMessage.Content = content;
        httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        httpResponseMessage.StatusCode = HttpStatusCode.OK;
        return httpResponseMessage;
    }

2 つ以上のドキュメント ID を試してみましたが、ダウンロードされたファイルは 1 つだけで、それも正しい形式ではありません (拡張子なし)。

4

2 に答える 2

2

唯一の方法は、すべてのファイルを圧縮してから、1 つの zip ファイルをダウンロードすることだと思います。使いやすいので、dotnetzipパッケージを使用できると思います。

1 つの方法は、最初にファイルをディスクに保存してから、zip をストリーミングしてダウンロードすることです。もう 1 つの方法は、それらをメモリに圧縮してから、ファイルをストリームでダウンロードすることです。

public ActionResult Download()
{
    using (ZipFile zip = new ZipFile())
    {
        zip.AddDirectory(Server.MapPath("~/Directories/hello"));

        MemoryStream output = new MemoryStream();
        zip.Save(output);
        return File(output, "application/zip", "sample.zip");
    }  
}
于 2018-09-10T04:31:52.970 に答える