返回文件为流。

huangapple go评论113阅读模式
英文:

Return file as stream

问题

I need when client send the id of user, I return file for it as stream. File may be image, PDF, video, or sound.

I'm using this code:

  1. [HttpGet]
  2. public async Task<FileStreamResult> GetAvatar(int id)
  3. {
  4. var result = await mediator.Send(new FindUserWithIdCommand { userId = id });
  5. if (result.Success)
  6. {
  7. return uploadService.GetFileStream(result.Result.AvatarName);
  8. }
  9. return null;
  10. }

Upload service:

  1. public FileStreamResult GetFileStream(string FileName)
  2. {
  3. try
  4. {
  5. var stream = File.OpenRead(Path.Combine(finder.PathAvatarUserUploadFolder(), FileName));
  6. FileStreamResult file = new FileStreamResult(stream, "application/octet-stream");
  7. return file;
  8. }
  9. catch (Exception ex)
  10. {
  11. throw;
  12. }
  13. }

but when I send a request for this action in Postman it shows me this result:

返回文件为流。

Where's the problem and how can I solve it?

英文:

I need when client send the id of user , i return file for it as stream. File may be image, PDF, video or sound.

I'm using this code:

  1. [HttpGet]
  2. public async Task&lt;FileStreamResult&gt; GetAvatar(int id)
  3. {
  4. var result = await mediator.Send(new FindUserWithIdCommand { userId = id });
  5. if (result.Success)
  6. {
  7. return uploadService.GetFileStream(result.Result.AvatarName);
  8. }
  9. return null;
  10. }

Upload service:

  1. public FileStreamResult GetFileStream(string FileName)
  2. {
  3. try
  4. {
  5. var stream = File.OpenRead(Path.Combine(finder.PathAvatarUserUploadFolder(), FileName));
  6. FileStreamResult file = new FileStreamResult(stream, &quot;application/octet-stream&quot;);
  7. return file;
  8. }
  9. catch (Exception ex)
  10. {
  11. throw;
  12. }
  13. }

but when i send a request for this action in postMan it show me this result:

返回文件为流。

Where's the problem and how can I solve it?

答案1

得分: 1

你的代码正在按预期执行。你所看到的是字节码,因为Postman不支持原生渲染PDF等内容。这就是文件;它只是以“原始”方式显示。

英文:

Your code is doing exactly what it is supposed to. What you're seeing is byte-code, because Postman doesn't support native rendering of things like PDFs. It is the file; it's just being displayed "raw".

huangapple
  • 本文由 发表于 2020年1月6日 15:44:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定