2016-05-04 141 views

回答

0

如果我正确理解这个问题,则需要添加一个MediaTypeFormatter来处理“application/pdf”格式。下面的例子...

public class PdfFormatter : MediaTypeFormatter 
{ 
    #region Constants and Fields 

    private const int ChunkSizeMax = 1024 * 10; 

    #endregion 

    #region Constructors and Destructors 

    public PdfFormatter() 
    { 
     SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/pdf")); 
    } 

    #endregion 

    #region Public Methods 

    public override bool CanReadType(Type type) 
    { 
     return false; // can't read any types 
    } 

    public override bool CanWriteType(Type type) 
    { 
     return type == typeof(byte[]); 
    } 

    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext) 
    { 
     Task t = new Task(() => WritePdfBytes(value, writeStream)); 
     t.Start(); 
     return t; 
    } 

    #endregion 

    #region Methods 

    private static void WritePdfBytes(object value, Stream writeStream) 
    { 
     byte[] buffer = value as byte[]; 
     int offset = 0; 

     while (offset < buffer.Length) 
     { 
      int chunkSize = Math.Min(buffer.Length - offset, ChunkSizeMax); 
      writeStream.Write(buffer, offset, chunkSize); 
      offset += chunkSize; 
     } 
    } 

    #endregion 
} 

在你的Global.asax.cs文件中添加

GlobalConfiguration.Configuration.Formatters.Add(新PdfFormatter());