将PDF Byte数组从Web API流式传输到浏览器

时间:2014-06-17 17:31:01

标签: pdf browser asp.net-web-api httpresponse

我在WebAPI中使用C#.net ...

我有...... WebAPI控制器......

public class GetSMBController : ApiController
{
    public HttpResponseMessage GET(int AgencyID, string UserName)
    {
        var Pdfs = new HttpResponseMessage();

        Pdfs = Utilities.Reporting.CreateStandardMapBooklet(AgencyID,UserName);

        return Pdfs;
    }

}

调用方法......

 public static HttpResponseMessage CreateStandardMapBooklet(int AgencyID, string userName)
    {
        string baseDataPath = @"\\dvfmweb4\\arcgisserver\basedata\";
        string tempDataPath = @"\\dvfmweb4\arcgisserver\tempGrowerData\";
        string unitTestSession = "UnitTestSession";
        int ShadeType = 1;
        bool isInImagery = false;
        double MaxSceneSize = 1.5;
        bool ScaleBar = true;
        int MapSettingID = 0;
        bool OverallPage = false;
        bool OverallImagery = false;
        bool IsRecordKeepingSheet = false;
        int RecordKeepingSheetID = 0;
        int TemplateID = 1480;



        RCIS.Mapping.MapDomain.Reports.StandardMapBookletReport _Pdfs = new RCIS.Mapping.MapDomain.Reports.StandardMapBookletReport(
                                                                                baseDataPath,
                                                                                tempDataPath,
                                                                                unitTestSession,
                                                                                AgencyID,
                                                                                ShadeType,
                                                                                isInImagery,
                                                                                MaxSceneSize,
                                                                                ScaleBar,
                                                                                MapSettingID,
                                                                                TemplateID,
                                                                                OverallPage,
                                                                                OverallImagery,
                                                                                false,
                                                                                RecordKeepingSheetID,
                                                                                userName,
                                                                                true);
        PDF testPDF = new PDF();
        byte[] test = null;
        try
        {
            List<RCIS.Mapping.MapDomain.Layout.CreatePdfResponse> pdf = _Pdfs.CreateReports();

            foreach (var item in pdf)
            {
                //PDFInfo info = new PDFInfo();
                //info.PdfBytes = item.PdfBytes;
                testPDF.Report = item.PdfBytes;
                test = item.PdfBytes;
            }

        }
        catch(Exception e)
        {

        }

        var response = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(test)
        };
        response.Content = new ByteArrayContent(test);
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
        {
            FileName = String.Format(AgencyID + userName + DateTime.Now.ToString("MMMddyyyy_HHmmss"))

        };

        response.Headers.CacheControl = new CacheControlHeaderValue()
        {
            MaxAge = new TimeSpan(0, 0, 60) // Cache for 30s so IE8 can open the PDF
        };





        return response;

    }

对于一些注释掉的东西和愚蠢的变量名称感到抱歉......我只是想看看这是否可行。

测试对象和HttpResponse消息内容已填充但是 我从浏览器中调用此错误时收到此错误...

This XML file does not appear to have any style information associated with it. The document tree is shown below.

发生了错误。

值不能为空。参数名称:内容

System.ArgumentNullException 位于D:\ FarmMaps \ Web \ WebAPI \ MappingServicesWebAPI \ MappingServicesWebAPI \ Utilities \ Reporting中MappingServicesWebAPI.Utilities.Reporting.CreateStandardMapBooklet(Int32 AgencyID,String userName)的System.Net.Http.ByteArrayContent..ctor(Byte []内容)。 cs:MappingServicesWebAPI.Controllers.GetSMBController.GET(Int32 AgencyID,String UserName)中的第92行,位于d:\ FarmMaps \ Web \ WebAPI \ MappingServicesWebAPI \ MappingServicesWebAPI \ Controllers \ MappingController.cs:第143行,位于lambda_method(Closure,Object,Object [ ])System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor。&lt;&gt; c__DisplayClass13.b__c(Object instance,Object [] methodParameters)at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance,Object) System.Web.Http.Controllers.ReflectedHttpActionDescriptor上的[] arguments)。&System; .Chreading.Tasks.TaskHelpers.RunSynchronously [TResult]的C__DisplayClass5.b__4()(Func`1 func,CancellationToken cancellationToken)

0 个答案:

没有答案