我正在创建一个代理服务器,该服务器向PDF Blob链接发出请求,然后接受该请求以设置我们发送给客户端的HttpResponse Header。
到目前为止,我已经成功地请求获取PDF格式的内容,但我不知道如何将其发送回用户。我已经关注了其他Stackoverflow的帖子,如这篇:https://stackoverflow.com/a/43232581/10541061
我将图中步骤3中的响应消息转换为流,并在新的HttpResponseMessage内容中将其发回。
- 我想返回给客户端的内容**
- 我实际返回给客户端的内容**
下面是我用来创建这个代理端点的代码
[AllowAnonymous]
[HttpGet("openPDF")]
public async Task<HttpResponseMessage> OpenPDF([FromQuery] string url)
{
var _httpClient = _httpClientFactory.CreateClient();
var response = await _httpClient.GetAsync(url);
var stream = await response.Content.ReadAsStreamAsync();
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
message.Content = new StreamContent(stream);
message.Content.Headers.ContentLength = stream.Length;
message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return message;
}
- 编辑**
当我这样写代理的时候,这个函数会返回PDF
[AllowAnonymous]
[HttpGet("openPDF")]
public async Task<FileStreamResult> OpenPDF([FromQuery] string url)
{
var fileStream = new MemoryStream();
var _httpClient = _httpClientFactory.CreateClient();
var file = await _httpClient.GetStreamAsync(url).ConfigureAwait(false);
await file.CopyToAsync(fileStream);
fileStream.Position = 0;
return File(fileStream, "application/pdf", "filename.pdf");
}
问题是我想将content-disposition
更新为inline
,这样我就可以强制它在浏览器中打开,而不是下载,所以我决定将文件流注入httpResponseMessage.content
,但这仍然不起作用,它将继续向我发送一个json文件
[AllowAnonymous]
[HttpGet("openPDF")]
public async Task<HttpResponseMessage> OpenPDF([FromQuery] string url)
{
var fileStream = new MemoryStream();
var _httpClient = _httpClientFactory.CreateClient();
var file = await _httpClient.GetStreamAsync(url).ConfigureAwait(false);
await file.CopyToAsync(fileStream);
fileStream.Position = 0;
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
message.Content = new StreamContent(fileStream);
message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return message;
}
老实说,我原以为定义内容类型就足够了,但我猜还不够
2条答案
按热度按时间nmpmafwu1#
这对于.NET6来说是非常直接的......我猜想对于.NET4x来说应该是大致相同的......这使用了NuGet包Azure.Storage.Blobs
https://github.com/Azure/azure-sdk-for-net/blob/Azure.Storage.Blobs_12.13.1/sdk/storage/Azure.Storage.Blobs/README.md
btqmn9zl2#
用于.NET 4x。
尝试添加: