• WebApi下载附件文件


    WebApi下载附件文件

    1.

    [RoutePrefix("down")]
        public class FilesController : ApiController
        {
    
            [GET("file/{id}")]
            public HttpResponseMessage GetSomePdf(string id)
            {
                var path = MyApp.PathToSomePdf(id);
                if (path!= null)
                    return FileAsAttachment(path, "somepdf.pdf");
                return new HttpResponseMessage(HttpStatusCode.NotFound);
            }
            public static HttpResponseMessage FileAsAttachment(string path, string filename)
            {
                if (File.Exists(path))
                {
    
                    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                    var stream = new FileStream(path, FileMode.Open);
                    result.Content = new StreamContent(stream);
                    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                    result.Content.Headers.ContentDisposition.FileName = filename;
                    return result;
                }
                return new HttpResponseMessage(HttpStatusCode.NotFound);
            }
    

    2.

    public class DataController : ApiController
       {
           // Sample content used to demonstrate range requests     
           private static readonly byte[] _content = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Content/airports.csv"));
     
           // Content type for our body
           private static readonly MediaTypeHeaderValue _mediaType = MediaTypeHeaderValue.Parse("text/csv");
     
           public HttpResponseMessage Get(bool IsLengthOnly)
           {
               //if only length is requested
               if (IsLengthOnly)
               {
                   return Request.CreateResponse(HttpStatusCode.OK, _content.Length);
               }
               else
               {               
                   MemoryStream memStream = new MemoryStream(_content);
     
                   // Check to see if this is a range request (i.e. contains a Range header field)               
                   if (Request.Headers.Range != null)
                   {
                       try
                       {
                           HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
                           partialResponse.Content = new ByteRangeStreamContent(memStream, Request.Headers.Range, _mediaType);
                           return partialResponse;
                       }
                       catch (InvalidByteRangeException invalidByteRangeException)
                       {
                           return Request.CreateErrorResponse(invalidByteRangeException);
                       }
                   }
                   else
                   {
                       // If it is not a range request we just send the whole thing as normal
                       HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK);
                       fullResponse.Content = new StreamContent(memStream);
                       fullResponse.Content.Headers.ContentType = _mediaType;
                       return fullResponse;
                   }
               }
           }
       }
    

      

  • 相关阅读:
    SpringBoot | Thymeleaf | 局部更新
    面试 | 冒泡排序优化
    JSP && Servlet | AXIS 0配置 入门
    155. 最小栈
    idea | 命名空间改过后重新导入项目方法
    Java | 基础归纳 | Map.Entry<String, String>
    08_Azkaban案例实践1_Command单一job示例
    07_Azkaban工作流调度器简介及其安装
    06_工作流调度器概述
    05_ Flume多级Agent之间串联案例
  • 原文地址:https://www.cnblogs.com/mxm2005/p/4881759.html
Copyright © 2020-2023  润新知