/// <summary>/// 傳回圖片 /// </summary>
[HttpGet]
[Route("api/User/GetImageFile")]
public HttpResponseMessage GetImageFile()
{
var imgPath = @"D:\00PowerData\Bird.fw.png";
var imgByte = File.ReadAllBytes(imgPath);
var imgStream = new MemoryStream(File.ReadAllBytes(imgPath));
var resp = new HttpResponseMessage(HttpStatusCode.OK) {
Content = new ByteArrayContent(imgByte)
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/*");
return resp;
}
/// <summary>/// 下載電子檔 /// </summary>[HttpGet] [Route("api/User/GetFile")] public HttpResponseMessage GetFile() { var imgName = "業務填單原始資料.xls"; var imgPath = string.Format(@"C:\Temp\{0}", imgName); var imgByte = File.ReadAllBytes(imgPath); var imgStream = new MemoryStream(File.ReadAllBytes(imgPath)); var resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(imgByte) }; resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/force-download"); resp.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = imgName }; return resp; }
/// <summary>
/// 傳回 json 資料
/// </summary>
[HttpGet]
[Route("api/Test/GetJson")]
public HttpResponseMessage GetJson()
{
var jsonStr = "{\"IsSuccess\":true,\"Data\":\"www.itdos.com\"}";
var result = new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(jsonStr, Encoding.UTF8, "text/json")
};
return result;
}
/// <summary>
/// 傳回文字資料(含 Html 文字也行)
/// </summary>
[HttpGet]
[Route("api/Test/GetHtml")]
public HttpResponseMessage GetHtml()
{
var str = "返回資料測試<b>返回資料測試</b> <a href='www.google.com'>www.google.com</a>";
var result = new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(str, Encoding.UTF8, "text/plain")
};
return result;
}
HTTP content-typ 可以參考 [這裡]
HTTP status code 可以參考 [這裡]
HTTP response header information 可以參考 [這裡]