C# 函数在开发计算机上工作,但在 Web 上失败

本文关键字:但在 Web 失败 工作 开发 计算机 函数 | 更新日期: 2023-09-27 18:33:18

我的Visual Studio Web应用程序中有这个函数,当我在开发机器上运行项目时,它似乎工作得很好,但是当我将其发布到Web并转到运行函数的页面时,它总是运行捕获异常部分。谁能帮忙?我在这里挠头:(下面是函数的代码。

public void printData()
{
    try
    {
        //The path to the Bid Form
        string pdfPath = Server.MapPath("PDF/Bid_Form.pdf");
        var pdfReader = new PdfReader(pdfPath);
        var pdfOutput = new MemoryStream();
        var pdfStamper = new PdfStamper(pdfReader, pdfOutput);
        CultureInfo ci = new CultureInfo("en-US");
        //This code block gets the date
        string date = BidDateTextBox.Value;
        DateTime dateNow = Convert.ToDateTime(date);
        string newDate = dateNow.ToString("MMMM dd, yyyy", ci);
        //This gets the values from the hidden fields
        decimal airTotal = (Convert.ToDecimal(at.Value) + Convert.ToDecimal(att.Value));
        decimal waterTotal = (Convert.ToDecimal(wt.Value) + Convert.ToDecimal(wtt.Value));
        decimal preTestAir = String.IsNullOrEmpty(AirPreTestTextBox.Value) ? 0 : decimal.Parse(AirPreTestTextBox.Value);
        decimal preTestWater = String.IsNullOrEmpty(WaterPreTestTextBox.Value) ? 0 : decimal.Parse(WaterPreTestTextBox.Value);
        decimal preTestCombine = preTestAir + preTestWater;
        decimal airTotalAll = (Convert.ToDecimal(at.Value) + Convert.ToDecimal(att.Value) + preTestAir);
        decimal waterTotalAll = (Convert.ToDecimal(wt.Value) + Convert.ToDecimal(wtt.Value) + preTestWater);
        //This line gets the total of all areas
        decimal combineTotal = Convert.ToDecimal(gt.Value);
        //This line gets the checked check boxes in the Per Specifications area
        string selectedSpecItems = (String.Join(",", CheckBoxList1.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text)) + ", " + CustomSpecTextBox.Value);
        //This line gets the checked check boxes in the Exclusions area
        string selectedExItems = (String.Join(",", CheckBoxList2.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text)) + ", " + CustomExcTextBox.Value);
        //This checks if the pre test check box is checked. If so it doesnt include the pretest amounts.
        if (PreTestCheckBox.Checked) {
            pdfStamper.AcroFields.SetField("PreTestAirBid", "$" + preTestAir);
            pdfStamper.AcroFields.SetField("PreTestWaterBid", "$" + preTestWater);
            pdfStamper.AcroFields.SetField("PreTestCombineBid", "$" + preTestCombine);
            pdfStamper.AcroFields.SetField("PreTestText", "Pre-Test");
            pdfStamper.AcroFields.SetField("AirBid", "$" + airTotal);
            pdfStamper.AcroFields.SetField("WaterBid", "$" + waterTotal);
            pdfStamper.AcroFields.SetField("CombineBid", "$" + combineTotal);
        }
        else
        {
            pdfStamper.AcroFields.SetField("PreTestAirBid", "");
            pdfStamper.AcroFields.SetField("PreTestWaterBid", "");
            pdfStamper.AcroFields.SetField("PreTestCombineBid", "");
            pdfStamper.AcroFields.SetField("AirBid", "$" + airTotalAll);
            pdfStamper.AcroFields.SetField("WaterBid", "$" + waterTotalAll);
            pdfStamper.AcroFields.SetField("CombineBid", "$" + combineTotal);
        }
        pdfStamper.AcroFields.SetField("Date", "" + newDate);
        pdfStamper.AcroFields.SetField("Bid#", "");
        pdfStamper.AcroFields.SetField("Location", "" + LocationTextBox.Value);
        pdfStamper.AcroFields.SetField("ProjectName", "" + ProjectNameTextBox.Value);
        pdfStamper.AcroFields.SetField("Engineer", "" + EngineerTextBox.Value);
        pdfStamper.AcroFields.SetField("EngineerPhone", "");
        pdfStamper.AcroFields.SetField("EngineerFax", "");
        pdfStamper.AcroFields.SetField("Architect", "" + ArchitectTextBox.Value);
        pdfStamper.AcroFields.SetField("ArchitectPhone", "");
        pdfStamper.AcroFields.SetField("ArchitectFax", "");
        pdfStamper.AcroFields.SetField("Specifications", "" + selectedSpecItems);
        pdfStamper.AcroFields.SetField("Exclusions", "" + selectedExItems);
        pdfStamper.FormFlattening = false;
        pdfStamper.Close();
        pdfReader.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=NewBid.pdf");
        Response.ContentType = "application/pdf";
        Response.BinaryWrite(pdfOutput.ToArray());
        Response.End();
    }
    catch (Exception e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('An error has occured.');", true);
    }
}

C# 函数在开发计算机上工作,但在 Web 上失败

在不知道异常的情况下,我的猜测是服务器在您引用的路径上没有对文件的适当访问权限。尝试在 catch 块中输出异常消息,而不是自定义错误消息。应该有帮助。

相关文章: