asp.net core API 3.1獲取圖片返回流文件


  [Route("{id}")]
        [HttpGet]
        public FileContentResult Get(string id)
        {
            using (var sw = new FileStream("E://項目//FileImag//20191217//11233aa89256ce39f4e378f58f3bd6e3ac537.jpg", FileMode.Open))
            {
                var contenttype = GetContentTypeForFileName("E://項目//FileImag//20191217//11233aa89256ce39f4e378f58f3bd6e3ac537.jpg");
                var bytes = new byte[sw.Length];
                sw.Read(bytes, 0, bytes.Length);
                sw.Close();
                return new FileContentResult(bytes, contenttype);
            }
        }
        /// <summary>
        /// 注冊表獲取文件類型
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private string GetContentTypeForFileName(string fileName)
        {
            //獲取文件后綴
            string ext = Path.GetExtension(fileName);
            using (Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext))
            {
                if (registryKey == null)
                    return null;
                var value = registryKey.GetValue("Content Type");
                return value?.ToString();
            }
        }
//直接返回文件
public IActionResult ShowImage()
{
  return PhysicalFile(@"c:\404.jpg", "image/jpeg");
}

 


免責聲明!

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



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