owin,webapi中将文件写入到response stream中

首先在controller项目中添加一个类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Net;
 6 using System.Net.Http;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 
10 namespace Test
11 {
12     public interface IWriteStreamToResponse<T>
13     {
14         T Suorce { get; set; }
15         void WriteToStream(Stream outputStream, HttpContent content, TransportContext context);
16     }
17 
18     public class StreamFromFileName : IWriteStreamToResponse<string>
19     {
20         private string _suorce;
21 
22         public string Suorce
23         {
24             get
25             {
26                 return _suorce;
27             }
28             set
29             {
30                 _suorce = value;
31             }
32         }
33         public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
34         {
35             try
36             {
37                 var buffer = new byte[65536];
38                 using (var video = File.Open(_suorce, FileMode.Open, FileAccess.Read))
39                 {
40                     var length = (int)video.Length;
41                     var bytesRead = 1;
42 
43                     while (length > 0 && bytesRead > 0)
44                     {
45                         bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
46                         await outputStream.WriteAsync(buffer, 0, bytesRead);
47                         length -= bytesRead;
48                     }
49                 }
50             }
51             catch (Exception ex)
52             {
53                 return;
54             }
55             finally
56             {
57                 outputStream.Close();
58             }
59         }
60     }
61 
62     public class StreamFromBytes : IWriteStreamToResponse<byte[]>
63     {
64         private byte[] _source;
65 
66         public byte[] Suorce
67         {
68             get
69             {
70                 return _source;
71             }
72             set
73             {
74                 _source = value;
75             }
76         }
77 
78         public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
79         {
80             try
81             {
82                 await outputStream.WriteAsync(_source, 0, _source.Length);
83             }
84             catch (Exception ex)
85             {
86                 return;
87             }
88             finally
89             {
90                 outputStream.Close();
91             }
92         }
93     }
94 }

controller中的action调用

 1  public HttpResponseMessage GetVideo(string file)
 2         {
 3             try
 4             {
 5                 var bytes = IC.GetClipFileBytes(file.Replace(".mp4", ""));
 6                 if (bytes == Encoding.UTF8.GetBytes(EntityEnums.StatusCode.NoFound.ToString()))
 7                     throw new HttpResponseException(HttpStatusCode.NotFound);
 8                 IWriteStreamToResponse<byte[]> video = new StreamFromBytes() { Suorce = bytes };
 9                 Action<Stream, HttpContent, TransportContext> send = video.WriteToStream;
10                 var response = Request.CreateResponse();
11                 response.Content = new System.Net.Http.PushStreamContent(send, new MediaTypeHeaderValue("video/mp4"));
12                 //调用异步数据推送接口
13                 return response;
14 
15             }
16             catch (Exception)
17             {
18                 throw;
19             }
20         }

 action中接收客户端upload的文件

  string tempFile = Environment.GetEnvironmentVariable("TEMP");
                MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(tempFile);
                var result = Request.Content.ReadAsMultipartAsync(provider).Result;
                string[] arrFiles = new string[provider.FileData.Count];
                int index = 0;
                //Request.Content.ReadAsMultipartAsync().Wait();
                // 获取文件名称  
                foreach (MultipartFileData fileData in provider.FileData)
                {
                }            

 

你可能感兴趣的:(owin,webapi中将文件写入到response stream中)