Web Api 返回圖片流給前端


 

 

    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;
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM