Solved

Redirecting to text file

Posted on 2016-09-07
11
31 Views
Last Modified: 2016-09-08
Why is this code not opening up a new window with the text file.  I've confirmed the file exists.  The problem seems to be with the response. write statement.

string UserFile = Page.User.Identity.Name.ToString();
                                        UserFile = UserFile.Replace(".", "");
                                        using (StreamWriter sw = new StreamWriter(DownloadDir + "gc\\Downloads\\" + UserFile + ".txt"))
                                        {
                                            foreach (DataColumn dc in ds.Tables["InputParameters"].Columns)
                                            {
                                                sw.Write(dc.ColumnName + "|");
                                            }
                                            sw.WriteLine("");
                                           
                                            foreach (DataRow dr in ds.Tables["InputParameters"].Rows)
                                            {
                                                ColCt = 0;
                                                foreach (DataColumn dc in ds.Tables["InputParameters"].Columns)
                                                {
                                                    sw.Write(dr[ColCt].ToString() + "|");

                                                    ColCt += 1;
                                                }
                                                sw.WriteLine("");
                                            }
                                    }

                                        Response.ContentType = "text/plain";
                                        Response.Write("<script>window.open('" + WebPath + "DS_SDI/gc/Downloads/" + UserFile + ".txt" + "','_blank')</script>");
                                        Response.End();

Open in new window

0
Comment
Question by:AlHal2
  • 5
  • 3
11 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 41788149
I assume there is no pop-up blocker and that javascript is enabled on the browser? If so, try hardcoding the javascript to make sure it works that way first.
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41788197
Why do these constructs look different?

(StreamWriter sw = new StreamWriter(DownloadDir + "gc\\Downloads\\" + UserFile + ".txt"))

Open in new window

Response.Write("<script>window.open('" + WebPath + "DS_SDI/gc/Downloads/" + UserFile + ".txt" + "','_blank')</script>");

Open in new window


Is DS_SDI a virtual directory published on the webserver pointing to DownloadDir?

Also, as SStory points out, ensure no popup blockers are enabled, use a static construct for folder in both locations to ensure they're working as expected.

Verify spelling and case of virtual directory in the code matches what is published on the server.  Case makes a difference in this situation, for the directory name, file name, and extension.
0
 

Author Comment

by:AlHal2
ID: 41789252
I tried hardcoding the javascript.  It failed.
I put the JavaScript into a text file which I saved as an html file.  It worked.
0
 

Author Comment

by:AlHal2
ID: 41789258
Pup up blocker is off.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Expert Comment

by:SStory
ID: 41789466
OK. Good. That is the way to debug it. Now you need to let the page generate the javascript, and in the browser do view source and see if that javascript looks like what you put in the html page exactly or if you missed anything.  Please post both the .HTML file and the "view source" output from the dynamically generated page.
0
 

Accepted Solution

by:
AlHal2 earned 0 total points
ID: 41789777
Thanks for your help.  I circumvented the problem by using blat.exe to email the relevant file.
0
 

Author Comment

by:AlHal2
ID: 41790129
I've requested that this question be closed as follows:

Accepted answer: 0 points for AlHal2's comment #a41789777

for the following reason:

Thanks for your help.  I circumvented the problem by using blat.exe to email the relevant file.
0
 
LVL 25

Expert Comment

by:SStory
ID: 41790130
AlHal2: I'm glad you solved your problem another way. Since, "how do I email a file via the command-line?" was not part of your question I don't see how it can be accepted as the answer.  I'm not sure that we have yet arrived at the solution, but just saying..
0
 

Author Comment

by:AlHal2
ID: 41790210
OK. Giving you points for effort.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get the web content with different encodings 2 28
Class object 2 27
Help with C#, MVC, razor. 6 23
Coding C# in Linux 8 33
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now