.Net Core 返回图片信息

admin 发布时间:2018-04-24 分类:.NET 阅读:10557次 5 条评论

public IActionResult GetImg(string code)
{
    #region 设置浏览器缓存header头信息
    Response.Headers.Add("Cache-Control", "pulic");
    Response.Headers.Add("Last-Modified", DateTime.Now.AddDays(-30).ToString("r"));
    Response.Headers.Add("Expires", DateTime.Now.AddDays(30).ToString("r"));
    Response.Headers.Add("ETag", code); 
    #endregion
    if (string.IsNullOrWhiteSpace(code))
    {
        
        //返回404信息
        return new StatusCodeResult((int)HttpStatusCode.NotFound);
    }
    var fileInfo = repository.GetList<B_UploadInfo>(new { FileCode = code }).FirstOrDefault();
    if (fileInfo == null)
    {
        return new StatusCodeResult((int)HttpStatusCode.NotFound);
    }
    try
    {
        var filePath = hostingEnv.ContentRootPath + "/wwwroot" + fileInfo.FilePath + fileInfo.FileCode + fileInfo.Extension;
        var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        var buffur = new byte[fileStream.Length];
        fileStream.Read(buffur, 0, (int)fileStream.Length);
        fileStream.Close();
        var fileResult = new FileContentResult(buffur, "image/jpeg");
        return fileResult;
    }
    catch (Exception ex)
    {
        logger.Error($"code:{code}," + ex);
        return new StatusCodeResult((int)HttpStatusCode.NotFound);
    }
}


已有5条留言

发表评论:

◎欢迎您的参与讨论。