简体   繁体   中英

How to open a local htm file on the server

Our application saves contract files to C:\\Contracts. The contracts are filled out on an aspx web page, and upon submission the contract is saved to that directory as a .htm file, and the filepath to the contract is stored in an OnlineCustomerDocuments object, which is then sent to the database. Later on, we want to be able to open these contracts, in the web browser through the web app. The customers are displayed in a gridview, with contract links in each row.

This is the code I have right now to open the contract:

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

Button to launch contract:

 <asp:LinkButton ID="btnPrintContract" runat="server"  CommandName="Print_Contract" Text="View Signed Contract" width="100px" CssClass ="GeneralHyperlink"></asp:LinkButton>

Basically, The selected row is grabbed, and the contract for that customer id is grabbed. When testing the application on my local machine, the contract is created, the db is updated properly, and the contract is able to be opened. However when I publish the app to the web server, I am unable to open the contract. The code executes up to Process.start(), but then nothing happens. I have checked file paths, and that the link button's __doPostBack is correct. The only thing that i can think of is that there is some permission issue. If that's the case would it be better to try and figure out how to grant those permissions using asp.net impersonation etc or is there a better way?

UPDATE *

So I tried to read in the html documents, and use response write like nathan suggested, but i really needed the contracts to open in a new window, and desoite trying several differtn window.open methods i couldnt get it to work. I ended up setting up a virtual directory as suggested, and everything works great now. Updated code below. Thanks for all of the help nathan.

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;
System.Diagnostics.Process.Start(strContractpath);

This is going to open the document on the server not on the client. Since you were testing locally it worked because you were the server and the client .

In other words, this line of code is simply starting a process on the server (in this case a web browser, as that is what is associated with a .htm file by default). When you tested this locally, you saw the page open because your computer was acting as the server , but this really isn't serving the page to the client, it's just opening a program. When you published this to the server you were not able to see the process starting because it happened on the server, got it?

What your really want to do is serve the contents of the file to the client through the webserver.

I'm trying to think of a way you can accomplish this.


You can create a virtual directory in IIS and point it at the directory containing the files, and then in your response do Response.Redirect(<URL to file>);

Or you can grab the contents of the file and stuff it into the response. I haven't ever done this personally but this SO post may point you in the right direction. This is probably the best option for you in my opinion because you wont have to alter any server settings.

Edit:

Here's an example of how you could accomplish this. Note that this is untested as I'm at work without a web environment.

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

     StreamReader sr = new StreamReader(strContractpath);
     while(sr.Peek() >= 0)
     {
          line=sr.ReadLine();
          Response.Write(line);
     }

     break;               
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM