我有一个web api,它从azure读取一个文件并将其下载到一个字节数组中。客户端接收此字节数组并将其下载为pdf。这对大文件不起作用。 我无法弄清楚如何将来自web api的块中的字节发送到客户端。
下面是web api代码,它只是将字节数组返回给客户端:
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
blockBlob.FetchAttributes();
byte[] data = new byte[blockBlob.Properties.Length];
blockBlob.DownloadToByteArray(data, 0);
return report;
客户端代码在ajax请求完成时获取数据,创建超链接并设置下载文件的下载属性:
var a = document.createElement("a");
a.href = 'data:application/pdf;base64,' + data.$value;;
a.setAttribute("download", filename);
1.86 MB的文件发生错误。
浏览器显示以下消息: 显示网页时出错。要继续,请重新加载网页。
答案 0 :(得分:1)
问题很可能是您的服务器在这些大文件上内存不足。不要将整个文件加载到变量中,然后将其作为响应发送出去。这导致双重下载,您的服务器必须从azure存储下载它并将其保存在内存中,然后您的客户端必须从服务器下载它。你可以做一个流来进行流复制,这样就不会对内存造成伤害。以下是WebApi控制器的示例。
public async Task<HttpResponseMessage> GetPdf()
{
//normally us a using statement for streams, but if you use one here, the stream will be closed before your client downloads it.
Stream stream;
try
{
//container setup earlier in code
var blockBlob = container.GetBlockBlobReference(fileName);
stream = await blockBlob.OpenReadAsync();
//Set your response as the stream content from Azure Storage
response.Content = new StreamContent(stream);
response.Content.Headers.ContentLength = stream.Length;
//This could change based on your file type
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
}
catch (HttpException ex)
{
//A network error between your server and Azure storage
return this.Request.CreateErrorResponse((HttpStatusCode)ex.GetHttpCode(), ex.Message);
}
catch (StorageException ex)
{
//An Azure storage exception
return this.Request.CreateErrorResponse((HttpStatusCode)ex.RequestInformation.HttpStatusCode, "Error getting the requested file.");
}
catch (Exception ex)
{
//catch all exception...log this, but don't bleed the exception to the client
return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Bad Request");
}
finally
{
stream = null;
}
}
我已经(几乎完全)使用了这段代码,并且能够下载超过1GB的文件。