public class TestController : ApiController { public HttpResponseMessage GetImg() { //獲取文件的絕對路徑 string absolutePath = System.Web.Hosting.HostingEnvironment.MapPath("/img/woman.jpg"); System.IO.FileStream fs = new System.IO.FileStream(absolutePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(fs), }; result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpg"); //下面這行代碼表示,彈出下載框,由用戶選擇需要保存的位置 //如果不寫,則直接打開該圖片 result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { //需要保存的圖片的默認名字,如果不寫,則默認提示為控制器名字,如本例為提示保存的名字為:"test" FileName = "newWoman.jpg" }; return result; } }