码迷,mamicode.com
首页 > Web开发 > 详细

netframework转core时文件响应流问题

时间:2018-05-31 19:11:52      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:info   one   class   art   问题   VID   tar   stack   ISE   

做将framework webapi项目转成netcore平台上的webapi项目时,发现原来的返回文件响应流在netcore平台下失效。代码如下,返回pdf文件响应流,供前端显示

    /// <summary>
        /// 根据pdf的预览id获取预览的pdf
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPreviewPdf")]
        public HttpResponseMessage GetPreviewPdf(Guid Id)
        {
            string pdfBase64String = _cacheManager.Get<string>(Id.ToString());
            var response = new HttpResponseMessage();
            if (!string.IsNullOrEmpty(pdfBase64String))
            {
                byte[] pdfArray = Convert.FromBase64String(pdfBase64String);

                response.StatusCode = System.Net.HttpStatusCode.OK;
                response.Content = new ByteArrayContent(pdfArray);
                response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(MimeTypes.ApplicationPdf);
            }
            else
            {
                response.StatusCode = System.Net.HttpStatusCode.Gone;
            }
            return response;

        }

不记得参考网址了,反正是stackoverflow上面了。需要在netcore项目中配置支持原有MVC的功能。

1、添加Nuget引用

技术分享图片

 

2、在startup类中添加配置

      public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddWebApiConventions();
}

 

netframework转core时文件响应流问题

标签:info   one   class   art   问题   VID   tar   stack   ISE   

原文地址:https://www.cnblogs.com/taoshengyujiu/p/9117808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!