我有两个API:
我需要实现一种在公共API中返回文件的方法。
DMZ中的API:
public HttpResponseMessage GetContent(int id)
{
var content = from m in db.messagestoimages
where m.Message == id
select m;
if (content == null || content.Count() == 0)
{
return null;
}
string fileName = content.First().ImageURL;
string fullPath = AppDomain.CurrentDomain.BaseDirectory + fileName;
if (File.Exists(fullPath))
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
var fileStream = new FileStream(fullPath, FileMode.Open);
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
return response;
}
return null;
}
公共API
public HttpResponseMessage Get(string id)
{
try
{
//if (form.file != null && form.file.Length > 0)
{
using (var client = new HttpClient())
{
try
{
string host = configuration.GetSection("MySettings").GetSection("OctopusURL").Value;
client.BaseAddress = new Uri(host);
var response = client.GetAsync("api/Content/" + id);
return response.Result;
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
}
}
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.NotFound); // 500 is generic server error
}
}
如果我直接调用DMZ方法,则文件下载成功。 如果我调用公共API,则不会得到文件。仅内容
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Length",
"value": [
"17488"
]
},
{
"key": "Content-Type",
"value": [
"application/octet-stream"
]
},
{
"key": "Expires",
"value": [
"-1"
]
},
{
"key": "Content-Disposition",
"value": [
"attachment; filename=\"/Userimage/3A297B090A41B649BF80.jpeg\""
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [
{
"key": "Cache-Control",
"value": [
"no-cache"
]
},
{
"key": "Pragma",
"value": [
"no-cache"
]
},
{
"key": "Server",
"value": [
"Microsoft-IIS/10.0"
]
},
{
"key": "X-AspNet-Version",
"value": [
"4.0.30319"
]
},
{
"key": "X-SourceFiles",
"value": [
"=?UTF-8?B?QzpcVXNlcnNcbmFyaW1cc291cmNlXHJlcG9zXENoYXRBc3Npc3RlbnRcQ2hhdEFzc2lzdGFudFxDaGF0QXNzaXN0ZW50XGFwaVxDb250ZW50XDkzMjQ=?="
]
},
{
"key": "X-Powered-By",
"value": [
"ASP.NET"
]
},
{
"key": "Date",
"value": [
"Wed, 21 Aug 2019 13:43:26 GMT"
]
}
],
"requestMessage": {
"version": {
"major": 2,
"minor": 0,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": null,
"method": {
"method": "GET"
},
"requestUri": "http://localhost:60236/api/Content/9324",
"headers": [],
"properties": {}
},
"isSuccessStatusCode": true
}
我该如何解决这个问题?
我尝试更改公共API,以便它获取Stream,创建响应并返回,但不起作用-结果相同。
string host = configuration.GetSection("MySettings").GetSection("OctopusURL").Value;
client.BaseAddress = new Uri(host);
var responseFromServer = await client.GetAsync("api/Images/" + id);
Stream streamToReadFrom = await responseFromServer.Content.ReadAsStreamAsync();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(streamToReadFrom);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "sd.jpg";
return response;
答案 0 :(得分:1)
经过测试,可以正常工作。 第一个控制器:
public class TestController : ApiController
{
[Route("GetContent")]
public HttpResponseMessage Get()
{
string fileName = "a.json";
string fullPath = AppDomain.CurrentDomain.BaseDirectory + fileName;
if (File.Exists(fullPath))
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
var fileStream = new FileStream(fullPath, FileMode.Open);
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
return response;
}
return null;
}
}
第二个控制器:
public class PublicController : ApiController
{
[Route("GetContent")]
public async Task<HttpResponseMessage> Get()
{
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri("http://localhost:9000/");
var responseFromServer = await client.GetAsync("api/Test/GetContent");
Stream streamToReadFrom = await responseFromServer.Content.ReadAsStreamAsync();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(streamToReadFrom);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "sd.json";
return response;
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
}
}
}
此对公共控制器方法的调用将下载文件:
答案 1 :(得分:0)
您应该通过公共API方法like this获取文件流,然后以DMZ返回文件流的相同方式将其返回给客户端。