我们的应用程序将合同文件保存到 C:\Contracts。合同在 aspx 网页上填写,并在提交时将合同作为 .htm 文件保存到该目录,合同的文件路径存储在 OnlineCustomerDocuments 对象中,然后将其发送到数据库。稍后,我们希望能够通过网络应用在网络浏览器中打开这些合约。客户显示在网格视图中,每一行都有合同链接。
这是我现在打开合同的代码:
protected void grdWebs_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intId = 0;
Web objWeb = new Web();
try
{
string strContractFilePath;
GridViewRow row1;
List<OnlineCustomerDocuments> lstOnlineCustomerDocuments;
OnlineCustomerDocuments contract;
string strContractpath;
switch (e.CommandName)
{
case "View_Details":
//GridViewRow row = (GridViewRow) (((Button)e.CommandSource).NamingContainer);
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
intId = Convert.ToInt32(grdWebs.DataKeys[row.RowIndex].Value.ToString());
Response.Redirect("DetailsForm.aspx?Id=" + intId.ToString(), false);
break;
case "Print_Contract":
strContractFilePath = ConfigurationManager.AppSettings["CustomerContractsDirectory"].ToString();
row1 = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
intId = Convert.ToInt32(grdWebs.DataKeys[row1.RowIndex].Value.ToString());
objWeb = QDServiceHelper._QDClient.GetDetails(intId);
lstOnlineCustomerDocuments = QDServiceHelper._QDClient.GetOnlineCustomerDocumentsByID(intId);
contract = lstOnlineCustomerDocuments.ElementAt(0);
strContractpath = contract.DocumentFileName;
System.Diagnostics.Process.Start(strContractpath);
break;
default:
break;
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
启动合约的按钮:
<asp:LinkButton ID="btnPrintContract" runat="server" CommandName="Print_Contract" Text="View Signed Contract" width="100px" CssClass ="GeneralHyperlink"></asp:LinkButton>
基本上,选定的行被抓取,并且该客户 ID 的合同被抓取。在我的本地机器上测试应用程序时,创建了合约,正确更新了数据库,并且能够打开合约。但是,当我将应用程序发布到 Web 服务器时,我无法打开合同。代码执行到 Process.start(),但没有任何反应。我检查了文件路径,并且链接按钮的 __doPostBack 是正确的。我唯一能想到的是存在一些权限问题。如果是这种情况,最好尝试弄清楚如何使用 asp.net 模拟等授予这些权限,还是有更好的方法?
更新*
因此,我尝试阅读 html 文档,并像 nathan 建议的那样使用响应写入,但我确实需要在新窗口中打开合同,并且尝试了几种不同的 window.open 方法,但我无法让它工作。我最终按照建议设置了一个虚拟目录,现在一切正常。更新了下面的代码。感谢所有帮助内森。
string strContractFilePath;
GridViewRow row1;
List<OnlineCustomerDocuments> lstOnlineCustomerDocuments;
OnlineCustomerDocuments contract;
string strfileName;
string file;
string pathlead;
string vDir = "/Webfiles/";
switch (e.CommandName)
{
case "View_Details":
//GridViewRow row = (GridViewRow) (((Button)e.CommandSource).NamingContainer);
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
intId = Convert.ToInt32(grdWebs.DataKeys[row.RowIndex].Value.ToString());
Response.Redirect("DetailsForm.aspx?Id=" + intId.ToString(), false);
break;
case "Print_Contract":
strContractFilePath = ConfigurationManager.AppSettings["CustomerContractsDirectory"].ToString();
row1 = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
intId = Convert.ToInt32(grdWebs.DataKeys[row1.RowIndex].Value.ToString());
lstOnlineCustomerDocuments = QDServiceHelper._QDClient.GetOnlineCustomerDocumentsByID(intId);
contract = lstOnlineCustomerDocuments.ElementAt(0);
strfileName = contract.DocumentFileName;
//path to the virtual directory on server
pathlead = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
if (HttpContext.Current.Request.Url.AbsoluteUri.Contains("CA"))
{
vDir = "/CA/WebFiles/CA1003/Contracts/";
file = pathlead + vDir + strfileName;
//Response.Redirect("~/WebFiles/contract.htm");
Response.Write("<script>");
Response.Write("window.open('" + file + "','_blank')");
Response.Write("</script>");
}
//For testing on local machines
else
{
System.Diagnostics.Process.Start(strContractFilePath + strfileName);
}
break;