[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

File download prob ASP.Net c#

Posted on 2008-11-03
30
Medium Priority
?
608 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:introlux
  • 14
  • 12
  • 3
  • +1
30 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22869031
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
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22869080
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
 

Author Comment

by:introlux
ID: 22874569
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:BToson
ID: 22875563
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
 
LVL 4

Expert Comment

by:BToson
ID: 22875567
Sorry, I meant "You can't redirect as you have terminated execution of the page using Response.End()."
0
 

Author Comment

by:introlux
ID: 22875603
Is there no function once user selects Save, or Open or cancel it will direct them to the desired page??
0
 
LVL 4

Expert Comment

by:BToson
ID: 22875618
You would have to use JavaScript and fire both events off at once or something simillar.
0
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22875699
-> 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
 

Author Comment

by:introlux
ID: 22876461
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
 

Author Comment

by:introlux
ID: 22876802
Also this code is not accepted:

        if (converttobool(Session["download"]) == true)
0
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22876890
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
 

Author Comment

by:introlux
ID: 22877133
Where would this be placed?? As I am getting confused
0
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22877180
page load event of the test.aspx page.
0
 

Author Comment

by:introlux
ID: 22877287
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
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22877317
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
 

Author Comment

by:introlux
ID: 22877335
Does not seem to like:

        Script += "\n<script language=JavaScript
        id='PopupWindow'>\n";
0
 

Author Comment

by:introlux
ID: 22877349
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
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22877397
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
 

Author Comment

by:introlux
ID: 22877433
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
 

Author Comment

by:introlux
ID: 22877937
the page should be Page

small cap not accepted. Now this compiles and does not do nothing
0
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22878164
have to blocked the popups in your brower.
0
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22878253
If it's not working yet send me the code.. I will try..
0
 

Author Comment

by:introlux
ID: 22884282
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
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22886549
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
 
LVL 3

Accepted Solution

by:
dhanushkad earned 2000 total points
ID: 22886686
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
 

Author Comment

by:introlux
ID: 22893220
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
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22895548
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
 

Author Comment

by:introlux
ID: 22895579
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
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22896061
yes thats bettre. Go for a button that is the best solution.
0
 

Author Closing Comment

by:introlux
ID: 31512772
Thanks for the help! Gone with a button that opens in a new window!

Thanks!
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question