gpt4 book ai didi

c# - 无法从 web api 获取文件

转载 作者:太空狗 更新时间:2023-10-30 01:00:47 24 4
gpt4 key购买 nike

我正在尝试通过获取请求发送文件,但此代码无效。我正在使用 .NET Core 并使用 Postman 测试 API。

// POST api/values
[HttpGet]
public HttpResponseMessage Get(string filename)
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream($"Scans/{filename}.obj", FileMode.Open, FileAccess.Read);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}

当我通过 postman 测试请求时,我得到这个而不是我的流:

{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"application/octet-stream"
]
},
{
"key": "Content-Length",
"value": [
"8785871"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}

最佳答案

HttpResponseMessage 来自上一版本的框架。您必须使用 IActionResult 实现的类。

您可以通过 Controller 返回文件流结果。

[HttpGet]
public IActionResult Get(string filename) {
var path = $"Scans/{filename}.obj";
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
var response = File(stream, "application/octet-stream"); // FileStreamResult
return response;
}

关于c# - 无法从 web api 获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43554860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com