【问题标题】:Server/Client File Stream PDF from Azure Function来自 Azure Function 的服务器/客户端文件流 PDF
【发布时间】:2021-10-19 00:44:30
【问题描述】:

我正在尝试从存储在 blob 存储中的 Azure 函数流式传输 PDF。这是服务器代码:

[Function("GetPdf")]
public async Task<IActionResult> GetPdf(
  [HttpTrigger(AuthorizationLevel.Anonymous, "post")],
  HttpRequestData req,
  FunctionContext executionContent)
{
  // Some code here to validate the body of the request
  // Some more code to deserialize the json from the body
  var reqBody = await new StreamReader(req.Body).ReadToEndAsynce();
  var theRequest = JsonConvert.DeserializeObject<TheRequest>(reqBody);
  var result = new ServiceResult<MemoryStream>();
  var ms = await svc.GetPdfFromBlobStorage(theRequest.fileName);
  ms.Seek(0, SeekOrigin.Begin);
  // I've tested that the actual pdf is in fact a pdf
  // by saving it to the drive before sending it to the client
  return new FileStreamResult(ms, "application/octet-stream");
}

这是客户端代码:

static async Task Main(string[] args)
{
  var fileInfo = new FileInfo(@"c:\temp\test.pdf");
  var client = new HttpClient();
  var req = new { fileName = "the_file"};
  var json = JsonConvert.SerializeObject(req);
  var data = new StringContent(json, Encoding.UTF8, "application/json");

  var response = await client.PostAsync("http://localhost:7071/api/GetPdf", data);
  
  await using var ms = await response.Content.ReadAsStreamAsync();
  await using var fs = File.Create(fileInfo.FullName);
  ms.Seek(0, SeekOrigin.Begin);
  ms.CopyTo(fs);
}

文件的内容不是pdf,而是纯文本:Microsoft.AspNetCore.Mvc.FileStreamResult

知道我做错了什么,无法在客户端接收流吗?

提前感谢您的帮助

【问题讨论】:

  • ITaskResult 是什么?我经常看到IActionResult
  • @ESG - 我的错...我修复了读取 IActionResult 的代码。
  • 您是否尝试过使用.ReadAsByteArrayAsyncByteArrayContent 而不是stringContent 根据本文档stackoverflow.com/a/46121344
  • 代码行await using var ms = await response.Content.ReadAsStreamAsync();应该是using var ms = await response.Content.ReadAsStreamAsync();吗?

标签: c# azure-functions filestream


【解决方案1】:

首先,我要感谢大家的意见。所提供的一切都使我们得到了正确的答案。以下是我们的工作方式:

服务器代码

[Function("GetPdf")]
public async Task<IActionResult> GetPdf(
  [HttpTrigger(AuthorizationLevel.Anonymous, "post")],
  HttpRequestData req,
  FunctionContext executionContent)
{
  // Some code here to validate the body of the request
  // Some more code to deserialize the json from the body
  var reqBody = await new StreamReader(req.Body).ReadToEndAsynce();
  var theRequest = JsonConvert.DeserializeObject<TheRequest>(reqBody);
  var result = new ServiceResult<MemoryStream>();
  var ms = await svc.GetPdfFromBlobStorage(theRequest.fileName);
  ms.Seek(0, SeekOrigin.Begin);
  // I've tested that the actual pdf is in fact a pdf
  // by saving it to the drive before sending it to the client
  return new FileContentResult(ms, "application/pdf") // change octet-stream to pdf
  {
    FileDownloadName = "test.pdf"
  }; 
}

我们创建了一个类来反序列化来自服务器的 json 结果

public class FileResponse
{
  public byte[] FileContents {get; set;}
  public string ContentType {get; set;}
  public string FileDownloadName {get; set;}
  public DateTimeOffset? LastModified {get; set;}
  public EntityTagHeaderValue EntityTag {get; set;}
  public string {get; set;}
}

最后是把魔法结合在一起的客户端代码

static async Task Main(string[] args)
{
  var client = new HttpClient();
  var req = new { prop1 = "someVal", prop2 = "AnotherVal", prop3 = "etc..."};
  var json = JsonConvert.SerializeObject(req);
  var data = new StringContent(json, Encoding.UTF8, "application/json");
  var httpRequest = new HttpRequestMessage
  {
    Method = HttpMethod.Post,
    RequestUri = new Uri("https://fakeurl/api/getpdf"),
    Content = data
  };
  client.DefaultRequestHeaders.Add("secret-key-if-your-api-is-secured", "the-secret");
  var response = JsonConvert.DeserializeObject<FileResponse>(await response.Content.ReadAsStringAsync());
  await using var ms = new MemoryStream(result.FileContents);
  await using var fs = File.Create($"c:\\temp\\{result.FileDownloadName}");
  await ms.CopytoAsync(fs);
}

我直接与 Daniel Arias (@daniel-morales-arias) 合作,他们发现了这个 article 并将最终代码放在一起

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2022-01-13
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    相关资源
    最近更新 更多