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

      

  • 相关阅读:
    在响应式项目中连接设计与开发
    社交APP的痛点及九大流派解析,微信陌陌,咱还约吗
    网传奶茶妹将进入红杉资本
    OLE、OCX和ActiveX控件之间的比较
    Unity3D网络游戏实战(第2版)
    离婚?在Facebook上把已婚状态改为单身就可以!
    同样酷炫但却失败了的谷歌眼镜,能否给Apple Watch一些前车之鉴?
    Apple Watch首批评测放出:有吐槽、有体贴……毒哭了,暖哭了
    如何培养战略领导力,赢得“长久游戏”?
    博客社交已死,数据社交“永生”
  • 原文地址:https://www.cnblogs.com/mxm2005/p/4881759.html
Copyright © 2020-2023  润新知