0

All, I am maintaining some old .ASP code for a website that requests a work order via email. The site worked properly until it was migrated recently to an IIS 7.5 Server. I had to recode the portion that does the file uploading in ASP.NET with C# code behind and am having trouble getting a String passed from new to old.

Previously, the .ASP code use the ASPupload component which, for sound reasons, is no longer supported on the new hosting Server. ASPupload used to pass the two uploaded filenames (via JavaScript as a Hidden Input value) to a section of the .ASP code that uses those filenames as a String (plus the path String that it already has). Then the .ASP code attaches the files to an email sent to the company fulfilling the work order.

I would like to take the new C# Global String Variable and pass it to the old .ASP code that does the emailing, since it still works like a charm.

I have evolved the Code Behind and ASP.NET on this question to reflect Manoj Kumar Sharma's excellent JavaScript that sends the C# Global String Variables to the ASP.NET code as a JavaScript Variable, which I was having trouble with initially. THANKS!!! And now we need to bridge the gap between the new ASP.NET code and the old .ASP code.

Here is the C# Code Behind (thanks for the edits Mason, it is now much easier to update this post):

// From https://support.microsoft.com/en-us/kb/323246
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CSharpUpload
{
    /// <summary>
    /// Browse to two files, upload to path on Server. Check if files haven't been downloaded, pass name.
    /// </summary>

    public class Scott_upload : System.Web.UI.Page
    {
        protected System.Web.UI.HtmlControls.HtmlInputFile File_Upload_1;
        protected System.Web.UI.HtmlControls.HtmlInputFile File_Upload_2;
        protected System.Web.UI.HtmlControls.HtmlInputButton Submit_Upload;

        public string UpFileName1 { get; set; }
        public string UpFileName2 { get; set; }
        public int File_Count;

        public bool Dont_show_buttons;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here

        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            // 
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            // 
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.Submit_Upload.ServerClick += new System.EventHandler(this.Submit_Upload_ServerClick);
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

        private void Submit_Upload_ServerClick(object sender, System.EventArgs e)
        {
            bool no_file_selected = true;

            File_Count = 0;

            if( ( File_Upload_1.PostedFile != null ) && ( File_Upload_1.PostedFile.ContentLength > 0 ) )
            {
                string fn_1 = System.IO.Path.GetFileName(File_Upload_1.PostedFile.FileName);
                string server_path_string_1 = @"Data\";
                string SaveLocation_1 = Server.MapPath(server_path_string_1) + fn_1;
                try
                {
                    File_Upload_1.PostedFile.SaveAs(SaveLocation_1);
                    this.UpFileName1 = fn_1;
                    File_Count = File_Count + 1;
                    Dont_show_buttons = true;
                    no_file_selected = false;

                    Response.Write("\nUpload Document 1 \"");
                    Response.Write(fn_1);
                    Response.Write("\" has been SUCCESSFULLY uploaded!\n");
                }
                catch ( Exception ex )
                {
                    Response.Write("Error: " + ex.Message);
                    //Note: Exception.Message returns a detailed message that describes the current exception. 
                    //For security reasons, we do not recommend that you return Exception.Message to end users in 
                    //production environments. It would be better to return a generic error message. 
                }
            }

            if( ( File_Upload_2.PostedFile != null ) && ( File_Upload_2.PostedFile.ContentLength > 0 ) )
            {
                string fn_2 = System.IO.Path.GetFileName(File_Upload_2.PostedFile.FileName);
                string server_path_string_2 = @"Data\";
                string SaveLocation_2 = Server.MapPath(server_path_string_2) + fn_2;
                try
                {
                    File_Upload_2.PostedFile.SaveAs(SaveLocation_2);
                    this.UpFileName2 = fn_2;
                    File_Count = File_Count + 1;
                    Dont_show_buttons = true;
                    no_file_selected = false;

                    Response.Write("\nUpload Document 2 \"");
                    Response.Write(fn_2);
                    Response.Write("\" has been SUCCESSFULLY uploaded!\n");
                }
                catch ( Exception ex )
                {
                    Response.Write("Error: " + ex.Message);
                    //Note: Exception.Message returns a detailed message that describes the current exception. 
                    //For security reasons, we do not recommend that you return Exception.Message to end users in 
                    //production environments. It would be better to return a generic error message. 
                }
            }

            if (no_file_selected)
            {
                Response.Write("\nPLEASE SELECT A FILE TO UPLOAD\n");
            }
        }
    }
}

. Here is the new ASP.NET which can see the filesnames as "upFileName1" and "upFileName2":

<%@ Page language="c#" src="Scott_upload.aspx.cs" AutoEventWireup="false" Inherits="CSharpUpload.Scott_upload"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>
  <HEAD>
    <title>Scott_upload</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="GridLayout">

        <form id="Scott_file_upload" method="post" enctype="multipart/form-data" runat="server">

            <% if (Dont_show_buttons == false) { %>
                <div>
                    Schedule A&B are required to complete the survey within 3 days.
                    <br><br>
                    Upload Document 1 <INPUT type=file id=File_Upload_1 name=File_Upload_1 runat="server" />
                    <br>
                </div>
                <div>
                    <br>
                    Upload Document 2 <INPUT type=file id=File_Upload_2 name=File_Upload_2 runat="server" />
                    <br>
                </div>
                <div>
                    <br><br>
                    <input type="submit" id="Submit_Upload" value="Click To Upload File(s)" NAME="Submit_Upload" runat="server" />
                    <br>
                </div>
            <% } %>
            <% else { %>
                <div>
                    <br><br>
                    Upload(s) COMPLETE.
                    <br>
                </div>
            <% } %>
        </form>

<script type="text/javascript">

    var upFileName1 = '<%= this.UpFileName1 %>';
    var upFileName2 = '<%= this.UpFileName2 %>';

    ' The code behind filenames are now appearing in the ASP.NET
    document.write(upFileName1);
    document.write(upFileName2);

</script>

</body>
</HTML>

And now for the third requested piece, the old .ASP code. Please forgive it, I did not write it, but must maintain it (I am much more specific with variable names). First is a section that calls "Scott_upload.aspx" from an iframe inside of a order request form (note the old Hidden Input "UploadedFile" that ASPupload used to use):

<html>
<!-- ... -->
<body>
<!-- ... -->
<table class="tblw" border="0" cellpadding="0" cellspacing="0" align="center" ID="Table5">
<tr>
<form name="frmRequest" method="post" action="format_email.asp">
<!-- ... -->
    <table class="tblw" border="0" cellpadding="0" cellspacing="0" align="center" ID="Table13">
    <tr>
        <input type="hidden" name="UploadedFile1" value="" ID="UploadedFile1id">
        <input type="hidden" name="UploadedFile2" value="" ID="UploadedFile2id">
        <td class="container" align="center">
            <iframe src="Scott_upload.aspx" width=520 height=140 name="upload" scrolling="yes" scrollbars="yes" frameborder="0" border="0" style="border:0px;padding:0px;margin:0px;overflow:visible;">
            </iframe>
        </td>
    </tr>
    </table>
<!-- ... -->
    <input type="button" name="btn" value="REQUEST ORDER" class="submitbtn" onClick="validateForm();">
</tr>
</form>
</table>

<% Server.Execute %>

</body>
</html>

Now, the old uploading code that someone else wrote did this bit of JavaScript manipulation to the Hidden Input Value (inside yet another iframe, inside yet another form, IKR?!) which does NOT work in my .ASPX:

<script language="javascript">

<% If Count = 1 Or Count = 2 Then %>
window.parent.document.getElementById("UploadedFile1id").value = document.frmUpload.FileName1.value;
<% End If %>

<% If Count = 2 Then %>
window.parent.document.getElementById("UploadedFile2id").value = document.frmUpload.FileName2.value;
<% End If %>

</script>

Then in "format_email.asp" the filename Strings are assigned with this line:

strUpload1 = Trim(Request.Form("UploadedFile1"))
strUpload2 = Trim(Request.Form("UploadedFile2"))

How do I get "upFileName1" and "upFileName2" from the "Scott_upload.aspx" page to assign to "strUpload1" and "strUpload2" in the old "format_email.asp" page?

4
  • 2
    I don't see your classic ASP code anywhere. Are you assuming that just because the extension is .aspx that it's classic ASP? Commented Jul 27, 2015 at 20:25
  • @mason, in my attempt at brevity and clarity, I left out the old code. But now here are all the details. Is the old code actually classic ASP, or am I confused? Commented Jul 30, 2015 at 20:02
  • 1
    If the extension is .aspx, then it's ASP.NET. If the extension is .asp, then it's classic ASP. Please adjust your tags and question appropriately. Commented Jul 30, 2015 at 20:07
  • And therein lies the root of my problem - I'm stuck interfacing both ASP.NET and classic ASP. Thanks for the clerification @mason. Commented Jul 31, 2015 at 14:07

1 Answer 1

1

You could try this:

Declare the two variables as Properties of the Page (something like this):

public class Scott_upload : System.Web.UI.Page
{
    public string UpFileName1 { get; set; }
    public string UpFileName2 { get; set; }
...

Then in the .ASPX page, where you have your javascript code, embed the values into javascript variables (something like this):

<script type="text/javascript">

    var upFileName1 = '<%= this.UpFileName1 %>';
    var upFileName2 = '<%= this.UpFileName2 %>';

</script>

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, a nice elegant bit of code that has moved me along, @ManojKumarSharma!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.