C#函数适用于开发机器但在Web上失败

时间:2014-12-05 13:40:00

标签: c# asp.net visual-studio

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

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);
    }

}

1 个答案:

答案 0 :(得分:0)

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