[英]c# application working on development machine, fails on non-development machine
[英]C# Function works on development machine but fails on web
我在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);
}
}
在不知道异常的情况下,我猜测服务器将无法正确访问您所引用路径中的文件。 尝试在catch块中输出异常消息,而不是您的自定义错误消息。 应该有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.