File download prob ASP.Net c#

Hi,

In my code behind, I have a file that pops up for the user to download - The problem is, after the file pops up asking the user to save or open file, the whole process in the code behind stops. Hence directing the user to another page fails!

Is there a method around this problem where regardless what happens, user is transfered to instructed page via using code:

Response.Redirect("Test.aspx");

Regards,

introlux
introluxAsked:
Who is Participating?
 
dhanushkadConnect With a Mentor Commented:
try with the this. It is working perfectly.  
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["download"] != null)
        {
            if ((bool)Session["download"] == true)
            {
                string PopUpWindowPage = "download.aspx";
 
                string Script = "";
                Script += "\n<script language=JavaScript id='PopupWindow'>\n";
                Script += "var confirmWin = null; \n";
                Script += "confirmWin = window.open('" + PopUpWindowPage + "','','width=455,height=435,status ');\n";
                Script += "</script>";
 
                if (!this.IsClientScriptBlockRegistered("PopupWindow"))
                {
                    this.RegisterStartupScript("PopupWindow", Script);
                }
            }
        }
    }

Open in new window

0
 
Anurag ThakurTechnical ManagerCommented:
i dont actually know how are you implementing your logic
but i will redirect to the test.aspx and in the page load i will write the logic to save or open the file and then take appropriate action

have a look at the following URL
http://www.aspfree.com/c/a/Code-Examples/File-download-using-C/

and if its still a problem then please share your code so that i can go through it to find any issues
0
 
dhanushkadCommented:
Use java script "window.open" method and load the instructed page via it.

<head>
<script type="text/javascript">
<!--
function downloadfile() {
window.open( "test.aspx" )
}
//-->
</script>
</head>
<body>
<form>
<input type="button" onClick="downloadfile()" value="Download">
</form>
</body>
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
introluxAuthor Commented:
I am using a an ASP.Net button - It is not only downloading the file. It is doing other stuff. Is there a mothod of opening the file instead of having the dialogue box displayed to the user (save as or open). Can it not just open the file straight away.

The file that is trying to be opened is a .ICS file. I have supplied a part of the code that I am trying to work with.

So basically would like the file CalendarEvent1.ics automatically opened and user to be directed to test.aspx aswell.
        if (lstStatus.SelectedValue.Equals("setup"))
        {
            if (CheckEmail() == false)
            {
                updateData();
                DelLockSession();
                CheckIssue();
                Response.Redirect("Test.aspx");
            }
            else
            {
                updateData();
                DelLockSession();
                CheckIssue();
                
                //-------------------------------------
 
                System.Text.StringBuilder sbICSFile =
                    new System.Text.StringBuilder();
                DateTime dtNow = DateTime.Now;
 
                sbICSFile.AppendLine("BEGIN:VCALENDAR");
                sbICSFile.AppendLine("VERSION:2.0");
                sbICSFile.AppendLine("PRODID:-//ICSTestCS/");
                sbICSFile.AppendLine("CALSCALE:GREGORIAN");
 
                // Define time zones.
                // GMT
                sbICSFile.AppendLine("BEGIN:VTIMEZONE");
                sbICSFile.AppendLine("TZID:GMT");
                sbICSFile.AppendLine("BEGIN:STANDARD");
                sbICSFile.AppendLine("DTSTART:20071104T020000");
                sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11");
                sbICSFile.AppendLine("TZOFFSETFROM:+0000");
                sbICSFile.AppendLine("TZOFFSETTO:+0000");
                sbICSFile.AppendLine("TZNAME:GMT");
                sbICSFile.AppendLine("END:STANDARD");
                sbICSFile.AppendLine("BEGIN:DAYLIGHT");
                sbICSFile.AppendLine("DTSTART:20070311T020000");
                sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3");
                sbICSFile.AppendLine("TZOFFSETFROM:+0000");
                sbICSFile.AppendLine("TZOFFSETTO:+0000");
                sbICSFile.AppendLine("TZNAME:GMT");
                sbICSFile.AppendLine("END:DAYLIGHT");
                sbICSFile.AppendLine("END:VTIMEZONE");
 
                // Define the event.
                sbICSFile.AppendLine("BEGIN:VEVENT");
 
                sbICSFile.AppendLine("SUMMARY:Test");
                sbICSFile.AppendLine("DESCRIPTION: Test");
                sbICSFile.AppendLine("PRIORITY:3");
                sbICSFile.AppendLine("UID:1");
                sbICSFile.AppendLine("SEQUENCE:0");
                sbICSFile.AppendLine("ATTENDEE;CN=test@test.com;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:test@test.com);
                sbICSFile.Append("DTSTAMP:" + dtNow.Year.ToString());
                sbICSFile.Append(FormatDateTimeValue(dtNow.Month));
                sbICSFile.Append(FormatDateTimeValue(dtNow.Day) + "T");
                sbICSFile.Append(FormatDateTimeValue(dtNow.Hour));
                sbICSFile.AppendLine(FormatDateTimeValue(dtNow.Minute) + "00");
 
                sbICSFile.AppendLine("END:VEVENT");
                sbICSFile.AppendLine("END:VCALENDAR");
 
                Response.ContentType = "text/calendar";
                Response.AddHeader("content-disposition",
                    "attachment; filename=CalendarEvent1.ics");
                Response.Write(sbICSFile);
                Response.End();
 
                //----------------------------
 
                Response.Redirect("test.aspx");

Open in new window

0
 
BTosonCommented:
You can't redirect as you have terminated execution of the page using Response.Redirect.
If you did not end the reponse when you did, then they page would end up being downloaded into the ICS file by the user.
0
 
BTosonCommented:
Sorry, I meant "You can't redirect as you have terminated execution of the page using Response.End()."
0
 
introluxAuthor Commented:
Is there no function once user selects Save, or Open or cancel it will direct them to the desired page??
0
 
BTosonCommented:
You would have to use JavaScript and fire both events off at once or something simillar.
0
 
dhanushkadCommented:
-> Create a seperate aspx file add the following codes  (see the Code Snippet) to page load event
e.g.  download.aspx

-> In the main page, base on your condition save a value (true/false) to session or pass with a query  string and redirect to Test.aspx page.

  if (lstStatus.SelectedValue.Equals("setup"))
        {
                updateData();
                DelLockSession();
                CheckIssue();

            if (CheckEmail() == false) ]
                       Session["download"] = true;
              else
                        Session["download"] = false;
         
}

-> Read the value in the test.aspx page load event and open a pop-up there

if ( converttobool(Session["download"] ) == true)
        // register java script block to open the download.aspx as a java script pop-up window.


http://coercedcode.blogspot.com/2007/08/opening-popup-windows-with-aspnet-and.html


 //-------------------------------------
 
                System.Text.StringBuilder sbICSFile =
                    new System.Text.StringBuilder();
                DateTime dtNow = DateTime.Now;
 
                sbICSFile.AppendLine("BEGIN:VCALENDAR");
                sbICSFile.AppendLine("VERSION:2.0");
                sbICSFile.AppendLine("PRODID:-//ICSTestCS/");
                sbICSFile.AppendLine("CALSCALE:GREGORIAN");
 
                // Define time zones.
                // GMT
                sbICSFile.AppendLine("BEGIN:VTIMEZONE");
                sbICSFile.AppendLine("TZID:GMT");
                sbICSFile.AppendLine("BEGIN:STANDARD");
                sbICSFile.AppendLine("DTSTART:20071104T020000");
                sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11");
                sbICSFile.AppendLine("TZOFFSETFROM:+0000");
                sbICSFile.AppendLine("TZOFFSETTO:+0000");
                sbICSFile.AppendLine("TZNAME:GMT");
                sbICSFile.AppendLine("END:STANDARD");
                sbICSFile.AppendLine("BEGIN:DAYLIGHT");
                sbICSFile.AppendLine("DTSTART:20070311T020000");
                sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3");
                sbICSFile.AppendLine("TZOFFSETFROM:+0000");
                sbICSFile.AppendLine("TZOFFSETTO:+0000");
                sbICSFile.AppendLine("TZNAME:GMT");
                sbICSFile.AppendLine("END:DAYLIGHT");
                sbICSFile.AppendLine("END:VTIMEZONE");
 
                // Define the event.
                sbICSFile.AppendLine("BEGIN:VEVENT");
 
                sbICSFile.AppendLine("SUMMARY:Test");
                sbICSFile.AppendLine("DESCRIPTION: Test");
                sbICSFile.AppendLine("PRIORITY:3");
                sbICSFile.AppendLine("UID:1");
                sbICSFile.AppendLine("SEQUENCE:0");
                sbICSFile.AppendLine("ATTENDEE;CN=test@test.com;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:test@test.com);
                sbICSFile.Append("DTSTAMP:" + dtNow.Year.ToString());
                sbICSFile.Append(FormatDateTimeValue(dtNow.Month));
                sbICSFile.Append(FormatDateTimeValue(dtNow.Day) + "T");
                sbICSFile.Append(FormatDateTimeValue(dtNow.Hour));
                sbICSFile.AppendLine(FormatDateTimeValue(dtNow.Minute) + "00");
 
                sbICSFile.AppendLine("END:VEVENT");
                sbICSFile.AppendLine("END:VCALENDAR");
 
                Response.ContentType = "text/calendar";
                Response.AddHeader("content-disposition",
                    "attachment; filename=CalendarEvent1.ics");
                Response.Write(sbICSFile);
                Response.End();
 
                //----------------------------

Open in new window

0
 
introluxAuthor Commented:
I have understood all the code apart from the java script block to open the download.aspx. Can you please provide more assistance on this.

Regards,

introlux
0
 
introluxAuthor Commented:
Also this code is not accepted:

        if (converttobool(Session["download"]) == true)
0
 
dhanushkadCommented:
just use

Session["NewForm"] == true

-> java script block

string PopUpWindowPage = "download.aspx";

string Script = "";
Script += "\n<script language=JavaScript
id='PopupWindow'>\n";
Script += "var confirmWin = null; \n";
Script += "confirmWin = window.open('"+
PopUpWindowPage+"','','width=455,height=435,status ');\n";
Script += "</script>";

if(!page.IsClientScriptBlockRegistered("PopupWindow"))
{
page.RegisterClientScriptBlock
("PopupWindow",Script);
}



0
 
introluxAuthor Commented:
Where would this be placed?? As I am getting confused
0
 
dhanushkadCommented:
page load event of the test.aspx page.
0
 
introluxAuthor Commented:
The code below:

Script += "\n<script language=JavaScript
id='PopupWindow'>\n";
Script += "var confirmWin = null; \n";
Script += "confirmWin = window.open('"+
PopUpWindowPage+"','','width=455,height=435,status ');\n";
Script += "</script>";

if(!page.IsClientScriptBlockRegistered("PopupWindow"))
{
page.RegisterClientScriptBlock
("PopupWindow",Script);
}

Does not seem to be accepted in the code behind.
0
 
dhanushkadCommented:
string PopUpWindowPage = "download.aspx";

string Script = "";
Script += "\n<script language=JavaScript
id='PopupWindow'>\n";
Script += "var confirmWin = null; \n";
Script += "confirmWin = window.open('"+
PopUpWindowPage+"','','width=455,height=435,status ');\n";
Script += "</script>";

if(!page.IsClientScriptBlockRegistered("PopupWindow"))
{
page.RegisterClientScriptBlock("PopupWindow",Script);
}
0
 
introluxAuthor Commented:
Does not seem to like:

        Script += "\n<script language=JavaScript
        id='PopupWindow'>\n";
0
 
introluxAuthor Commented:
it allowed it when I put both on the same line.

The next error i get:

Compiler Error Message: CS0103: The name 'page' does not exist in the current context

Source Error:

 

Line 27:         Script += "</script>";
Line 28:
Line 29:         if(!page.IsClientScriptBlockRegistered("PopupWindow"))
Line 30:         {
Line 31:         page.RegisterClientScriptBlock("PopupWindow",Script);
 
0
 
dhanushkadCommented:
string Script = "";
Script += "\n<script language=JavaScriptid='PopupWindow'>\n";
Script += "var confirmWin = null; \n";
Script += "confirmWin = window.open('"+ PopUpWindowPage +"','','width=455,height=435,status ');\n";
Script += "</script>";

try now. Sorry I dont have VS right now to verify it.
0
 
introluxAuthor Commented:
Compiler Error Message: CS0103: The name 'page' does not exist in the current context

Source Error:

 

Line 26:         Script += "</script>";
Line 27:
Line 28:         if(!page.IsClientScriptBlockRegistered("PopupWindow"))
Line 29:         {
Line 30:         page.RegisterClientScriptBlock("PopupWindow",Script);
0
 
introluxAuthor Commented:
the page should be Page

small cap not accepted. Now this compiles and does not do nothing
0
 
dhanushkadCommented:
have to blocked the popups in your brower.
0
 
dhanushkadCommented:
If it's not working yet send me the code.. I will try..
0
 
introluxAuthor Commented:
Still cant seem to execute this. I have supplied my code.
//Main Page
//==============================================================================
 
public partial class EditPageTest: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string PopUpWindowPage = "testPara2.aspx";
 
        string Script = "";
        Script += "\n<script language=JavaScriptid='PopupWindow'>\n";
        Script += "var confirmWin = null; \n";
        Script += "confirmWin = window.open('" + PopUpWindowPage + "','','width=455,height=435,status ');\n";
        Script += "</script>";
 
        if (!Page.IsClientScriptBlockRegistered("PopupWindow"))
        {
            Page.RegisterClientScriptBlock("PopupWindow", Script);
        }
    }
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        if (opProStatusDrop.SelectedValue.Equals("Open - setup"))
        {
            if (CheckPMsendEmail() == false)
            {
                updateData();
                DelLockSession();
                CheckIssue();
                Session["download"] = false;
                Response.Redirect("SessPrint.aspx");
            }
            else
            {
                updateData();
                DelLockSession();
                CheckIssue();
                Session["download"] = true;
                Response.Redirect("SessPrint.aspx");
            }
        }
}
 
//Test Page (window that will display this page)
//======================================================================
public partial class testPara2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
//-------------------------------------
 
                System.Text.StringBuilder sbICSFile =
                    new System.Text.StringBuilder();
                DateTime dtNow = DateTime.Now;
 
                sbICSFile.AppendLine("BEGIN:VCALENDAR");
                sbICSFile.AppendLine("VERSION:2.0");
                sbICSFile.AppendLine("PRODID:-//ICSTestCS/");
                sbICSFile.AppendLine("CALSCALE:GREGORIAN");
 
                // Define time zones.
                // GMT
                sbICSFile.AppendLine("BEGIN:VTIMEZONE");
                sbICSFile.AppendLine("TZID:GMT");
                sbICSFile.AppendLine("BEGIN:STANDARD");
                sbICSFile.AppendLine("DTSTART:20071104T020000");
                sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11");
                sbICSFile.AppendLine("TZOFFSETFROM:+0000");
                sbICSFile.AppendLine("TZOFFSETTO:+0000");
                sbICSFile.AppendLine("TZNAME:GMT");
                sbICSFile.AppendLine("END:STANDARD");
                sbICSFile.AppendLine("BEGIN:DAYLIGHT");
                sbICSFile.AppendLine("DTSTART:20070311T020000");
                sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3");
                sbICSFile.AppendLine("TZOFFSETFROM:+0000");
                sbICSFile.AppendLine("TZOFFSETTO:+0000");
                sbICSFile.AppendLine("TZNAME:GMT");
                sbICSFile.AppendLine("END:DAYLIGHT");
                sbICSFile.AppendLine("END:VTIMEZONE");
 
                // Define the event.
                sbICSFile.AppendLine("BEGIN:VEVENT");
 
                sbICSFile.AppendLine("SUMMARY:Test");
                sbICSFile.AppendLine("DESCRIPTION: Test");
                sbICSFile.AppendLine("PRIORITY:3");
                sbICSFile.AppendLine("UID:1");
                sbICSFile.AppendLine("SEQUENCE:0");
                sbICSFile.AppendLine("ATTENDEE;CN=test@test.com;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:test@test.com");
                sbICSFile.Append("DTSTAMP:" + dtNow.Year.ToString());
                sbICSFile.Append(FormatDateTimeValue(dtNow.Month));
                sbICSFile.Append(FormatDateTimeValue(dtNow.Day) + "T");
                sbICSFile.Append(FormatDateTimeValue(dtNow.Hour));
                sbICSFile.AppendLine(FormatDateTimeValue(dtNow.Minute) + "00");
 
                sbICSFile.AppendLine("END:VEVENT");
                sbICSFile.AppendLine("END:VCALENDAR");
 
                Response.ContentType = "text/calendar";
                Response.AddHeader("content-disposition",
                    "attachment; filename=CalendarEvent1.ics");
                Response.Write(sbICSFile);
                Response.End();
 
                //----------------------------
    }
}

Open in new window

0
 
dhanushkadCommented:
Move the Page_Load implentation of the main page into the SessPrint.aspx as follows.
Page_Load of the main page should be blank.

if (Session["download"] == true)
{
        string PopUpWindowPage = "testPara2.aspx";
         string Script = "";
        Script += "\n<script language=JavaScriptid='PopupWindow'>\n";
        Script += "var confirmWin = null; \n";
        Script += "confirmWin = window.open('" + PopUpWindowPage + "','','width=455,height=435,status ');\n";
        Script += "</script>";
 
        if (!Page.IsClientScriptBlockRegistered("PopupWindow"))
        {
            Page.RegisterClientScriptBlock("PopupWindow", Script);
        }

}
0
 
introluxAuthor Commented:
It is working on my local machine. For some strange reason, when I upload it onto the server. The pop up opens and then closes automatically.

Any idea why this is happening, even though I dont have any pop up stopper installed??
0
 
dhanushkadCommented:
Should be something wrong with the testPara2.aspx.

Try to comment the implementation of the testPara2.aspx and see if the popup window load the stay open. if this case the problem should be with the file download code block.

0
 
introluxAuthor Commented:
The pop up works. For some strange reason, it does not like to execute the download file from load up. IE seems to just close the browser. So I may have to result in a button displayed:

DOWNLOAD
0
 
dhanushkadCommented:
yes thats bettre. Go for a button that is the best solution.
0
 
introluxAuthor Commented:
Thanks for the help! Gone with a button that opens in a new window!

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.