Md *_*lam 1 c# web-applications azure asp.net-web-api2 httpresponsemessage
我有一个 Web Api 控制器方法,可以获取传递的文档 ID,并且它应该为这些请求的 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;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用 2 个或更多文档 ID,但只下载了一个文件,而且格式也不正确(无扩展名)。
压缩是在所有浏览器上具有一致结果的唯一选项。MIME/multipart 内容用于电子邮件消息 ( https://en.wikipedia.org/wiki/MIME#Multipart_messages ),并且从未打算在 HTTP 事务的客户端上接收和解析它。有些浏览器确实实现了它,有些则没有。
或者,您可以更改 API 以接受单个 docId,并从客户端为每个 docId 迭代 API。
归档时间: |
|
查看次数: |
10843 次 |
最近记录: |