我有一个Intranet WebAPI服务器,它为DMZ(Internet)服务器提供服务。
API操作:
[HttpGet]
public HttpResponseMessage ImagePath(string filePath)
{
var result = new HttpResponseMessage(HttpStatusCode.OK);
FileStream fileStream = new FileStream(filePath, FileMode.Open);
Image image = Image.FromStream(fileStream);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
result.Content = new ByteArrayContent(memoryStream.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return result;
}
api确实成功返回了一张图片。 我想从DMZ向Intranet发出请求,但我不知道如何编写动作,我的代码如下:
public async Task<HttpResponseMessage> ImagePath(string filePath)
{
HttpClientHandler handler = new HttpClientHandler()
{
UseDefaultCredentials = true,
};
using (var client = new HttpClient(handler)) {
client.BaseAddress = new Uri(apiUrl); //apiUrl is my intranet domain
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("image/jpeg"));
HttpResponseMessage fs = await client.GetAsync("cipap/api/baseapi/ImagePath?filePath=" + filePath);
return fs;
}
//FileStream fs = new FileStream(filePath, FileMode.Open);
//return File(fs, "image/jpeg");
}
有人能帮帮我吗?非常感谢。
答案 0 :(得分:0)
我终于找到了anwser,
只需进行FileResult类型的操作,
并通过以下方式返回文件:
Model
还有一件事是,在webapi操作中,我需要按如下方式配置文件流,否则文件将在第一次请求后被锁定:
class Model extends commonfunctions {
public function __construct(){
parent::__construct();
}
}
File(fs.Content.ReadAsByteArrayAsync().Result, "image/jpeg");
REF: Download file prompt when using WebAPI HttpResponseMessage