英文:
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<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 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".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论