【问题标题】:Why can't StorageFile.GetFileFromPathAsync load from an exsisting PDF file为什么不能从现有 PDF 文件加载 StorageFile.GetFileFromPathAsync
【发布时间】:2022-01-17 07:25:09
【问题描述】:

我正在尝试将从 SQL ReportServer 生成的 PDF 作为 PdfDocument 传递回 UWP 应用程序。尝试创建 StorageFile

时,我不断收到异常
[HttpGet]
[Route("invoicereport/{report}/{invoice}")]
public async Task<IHttpActionResult> GetInvoiceReport([FromUri] string report, [FromUri] string invoice)
{
    try
    {
        Debug.WriteLine("Test");

        ReportExecutionService rs = new ReportExecutionService();

        rs.Credentials = CredentialCache.DefaultCredentials;

        rs.Url = "http://localhost/reportserver/reportexecution2005.asmx";

        rs.ExecutionHeaderValue = new ExecutionHeader();
        var executionInfo = new ExecutionInfo();
        executionInfo = rs.LoadReport($"{ReportsDir}/Invoice/{report}", null);
        

        List<ParameterValue> parameters = new List<ParameterValue>();

        parameters.Add(new ParameterValue { Name = "SOPNUMBER", Value = invoice });
        parameters.Add(new ParameterValue { Name = "SOPTypeString", Value = "Invoice" });

        rs.SetExecutionParameters(parameters.ToArray(), "en-US");

        string deviceInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
        string mimeType;
        string encoding;
        string[] streamId;
        Warning[] warning;

        var result = rs.Render("PDF", deviceInfo, out mimeType, out encoding, out encoding, out warning, out streamId);

        FileStream stream = File.Create(System.Web.HttpRuntime.CodegenDir + $"/{invoice}.pdf", result.Length);

        //write file with rendered result

        stream.Write(result, 0, result.Length);

        //close stream

        stream.Close();

        StorageFile file = StorageFile.GetFileFromPathAsync(System.Web.HttpRuntime.CodegenDir + $"/{invoice}.pdf").GetResults();

        var pdf = PdfDocument.LoadFromFileAsync(file).GetResults();

        return Ok(pdf);

    }
    catch (Exception ex)
    {
        Log.Logger.Error(ex, "Reporting Error");
    }

    return Ok();
}

异常被记录为:

System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
   at Windows.Foundation.IAsyncOperation`1.GetResults()
   at Prism.Web.Queries.Controllers.ReportController.<GetInvoiceReport>d__6.MoveNext() in C:\Projects\PointOfSaleBeta\Prism.Web.Queries\Controllers\ReportController.cs:line 183

第183行对应StorageFile file = StorageFile.GetFileFromPathAsync(System.Web.HttpRuntime.CodegenDir + $"/{invoice}.pdf").GetResults();

我已经(多次)验证了引用的 PDF 文件实际上是创建的。我没有为“GetFileFromPathAsync”使用正确的语法吗?

【问题讨论】:

    标签: c# pdf asp.net-web-api storagefile


    【解决方案1】:

    在我没有得到任何答案或 cmets 后,我尝试了不同的方法。我安装了 FreeSpire.PDF nuget 包:

    [HttpGet]
    [Route("invoicereport/{report}/{invoice}")]
    public async Task<IHttpActionResult> GetInvoiceReport([FromUri] string report, [FromUri] string invoice)
    {
        try
        {
            Debug.WriteLine("Test");
    
            ReportExecutionService rs = new ReportExecutionService();
    
            rs.Credentials = CredentialCache.DefaultCredentials;
    
            rs.Url = "http://localhost/reportserver/reportexecution2005.asmx";
    
            rs.ExecutionHeaderValue = new ExecutionHeader();
            var executionInfo = new ExecutionInfo();
            executionInfo = rs.LoadReport($"{ReportsDir}/Invoice/{report}", null);
            
    
            List<ParameterValue> parameters = new List<ParameterValue>();
    
            parameters.Add(new ParameterValue { Name = "SOPNUMBER", Value = invoice });
            parameters.Add(new ParameterValue { Name = "SOPTypeString", Value = "Invoice" });
    
            rs.SetExecutionParameters(parameters.ToArray(), "en-US");
    
            string deviceInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
            string mimeType;
            string encoding;
            string[] streamId;
            Warning[] warning;
    
            var result = rs.Render("PDF", deviceInfo, out mimeType, out encoding, out encoding, out warning, out streamId);
    
            PdfDocument pdf = new PdfDocument();
    
            pdf.LoadFromBytes(result);
    
            
    
            return Ok(pdf);
    
        }
        catch (Exception ex)
        {
            Log.Logger.Error(ex, "Reporting Error");
        }
    
        return Ok();
    }
    

    这解决了(解决)我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多