返回文件为流。

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

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:

[HttpGet]
public async Task<FileStreamResult> GetAvatar(int id)
{
    var result = await mediator.Send(new FindUserWithIdCommand { userId = id });
    if (result.Success)
    {
        return uploadService.GetFileStream(result.Result.AvatarName);
    }
    return null;
}

Upload service:

public FileStreamResult GetFileStream(string FileName)
{
    try
    {
        var stream = File.OpenRead(Path.Combine(finder.PathAvatarUserUploadFolder(), FileName));
        FileStreamResult file = new FileStreamResult(stream, "application/octet-stream");
        return file;
    }
    catch (Exception ex)
    {
        throw;
    }
}

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:

[HttpGet]
public async Task&lt;FileStreamResult&gt; GetAvatar(int id)
{
    var result = await mediator.Send(new FindUserWithIdCommand { userId = id });
    if (result.Success)
    {
        return uploadService.GetFileStream(result.Result.AvatarName);
    }
    return null;
}

Upload service:

public FileStreamResult GetFileStream(string FileName)
{
    try
    {
        var stream = File.OpenRead(Path.Combine(finder.PathAvatarUserUploadFolder(), FileName));
        FileStreamResult file = new FileStreamResult(stream, &quot;application/octet-stream&quot;);
        return file;
    }
    catch (Exception ex)
    {
        throw;
    }
}

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:

确定