Open a new page using HTML Response/JS ASP - asp.net

Alright, I have decided to go about this by opening a new page (newPage.aspx) which will then initiate the download and close after the download is completed. I am opening newPage.aspx using javascript by writing it to the response of the current page. For some reason, however, the window is not being opened before the original page is re-directed. Is their a method to be called before I redirect? Maybe I have a syntax issue?
Response.Write("<script type='text/javascript'>window.open('~/newPage.aspx', '', ''); </script>")
Response.Redirect("~/oldPage.aspx")
EDIT:
I tried the following but it did not work... (I am working in an Update Panel)
ScriptManager.RegisterStartupScript(udpMain, udpMain.GetType(), "openExcel", "window.open('~/newPage.aspx', '' , '');", True)
EDIT 2: So Close
Ok, so this only works if I comment out the Response.Redirect. It seems that the RegisterStartupScript method takes place on the page load after the Response.Redirect method. Is there any known fix for this?
ScriptManager.RegisterStartupScript(udpMain, udpMain.GetType(), "openExcel", "window.open(NewPage.aspx'); location.href='OldPage.aspx';", True)
Response.Redirect("~/OldPage.aspx")

You can redirect via client script as well. By the way when using ASP.NET preferable way of embedding JS into HTML is ClientScript.RegisterStartupScript e.g
ClientScript.RegisterStartupScript(Me.GetType(), "JSCode" & Rnd(1).ToString, "window.open('newPage.aspx'); location.href = 'oldPage.aspx'", True)

Related

Issues with Kendo MVVM file upload control

I was trying to get a Kendo MVVM file upload working along with ASP.NET. This is how my HTML looks for the upload declaration:
<input name="attachments"
id="fileUpload"
type="file"
data-role="upload"
data-async="{ saveUrl: 'FileUpload.aspx', autoUpload: true }"
data-bind="events: { success: onSuccess,error: onError }">
and the FileUpload page load:
Response.Expires = -1;
//Code to upload -- This returns me the file url that i need to send back as a response
Response.ContentType = "text/plain";
Response.Write(fileUrl);
Response.End();
The page load written above does function as expected and return me the desired result but the kendo control here behaves in a funny manner. It tells me that the upload is unsuccessful as in the error icon shows up on the UI. Moreover it is the error handler provided by Kendo that is executed though the written response is returned properly when I try accessing it as:
e.XMLHttpRequest.responseText
Well I guess I might have missed out something/done some small mistake here or there but unfortunately I am not able to figure the same out. Anyone who could possibly suggest/correct?
Well it seems that the response for the file upload control should be empty or a JSON string otherwise it is treated as an error. I changed the Response text to thus:
Response.Write(new JavaScriptSerializer().Serialize(fileUrl));
to resolve.
Hope this helps someone else!

window.open and window.location in the same script

I have a small problem, I have a linkbutton that I am linking to a public sub that does the following:
Dim url As String = "articledownload.aspx?articleID=" & LatestNewsLetter.ArticleID
Page.ClientScript.RegisterStartupScript(Me.GetType(), "OpenWin", "<script>window.open('" & url & "'); window.location('newsReleases.aspx')</script>")
This works with IE, but not chrome. I have tried switching the order of the scripts around but with the same results. I am trying to open a new window with a file download and at the same time redirect the current page to another location. Is there another method of accomplishing this?
Use
window.location.href = 'newsReleases.aspx';
In order to do the second bit of redirecting the main page.
JSFiddle.

Web form non-responsive after download

I'm working in ASP.NET (2.0) and we have a page where a user is able to select and download a series of files as a zip. I got this to work without undo difficulty by using the DotNetZip library (which is probably not relevant to the problem, but included for completeness.)
After the user checks which files they want to download, the page does a postback, and in the button click event handler, I use the following code:
Response.Clear();
Response.BufferOutput = false; // false = stream immediately
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=FileRequest.zip");
using (ZipFile zip = new ZipFile())
{
zip.AddFile(MapPath("/datafiles/fileToZip.pdf"), "");
zip.Save(Response.OutputStream);
}
Response.Close();
And this all seems to work great. The user clicks the button, a download window pops up, the user downloads the zip. All is good...
...until they decide they want to do something else on the page. The buttons in the form are no longer responsive. For instance, if I click the download button again, it does nothing. If I reload the page, I can repeat this behavior...it works once, then does nothing.
I'm not understanding why the browser doesn't send the new request. It's not "spinning" or otherwise acting busy. I thought that this might be a browser issue, but I've repeated it in both IE and Firefox, so it seems likely that it's something I'm not understanding. Strangely, it's only form submission elements that seem to be non-responsive. Javascript still works, and so do regular links.
So why is this happening, and how do I get around it?
The problem is likely down to you returning;
Response.ContentType = "application/zip";
Once this has been sent (along with the actual content), the browser would assume it has nothing left to do (I believe).
You probably should create a new window specifically for downloading the files by saving the file selection in a session parameter (or equivalent) and opening a popup window that has your download code in.
This will leave the parent page in a suitable state.
Content-disposition header seems to be a discouraged solution. But the same effect of ASP.NET forms not being responsive occurs if you use standard Redirect to a zip file.
I solved very similar problem (returning *.csv files by the server for download) using Tančev Saša's code in "Response.Redirect to new window" Q&A and it works great. Perhaps it might produce some popup warnings in some browsers, but I think this is how it's often done in download sites.

JS window.close() let IE hang

for reason I won't bore you with, I'm writing an asp.net application that must open some pages in new browser windows.
I managed to open them within a postback (don't ask why, I just needed to) with this code:
script = String.Format(#"window.open(""{0}"", ""{1}"");", url, target);
ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);
Now I have new windows each one with a button that should close it. I have simply an onclick="window.close()" (but that prompts me when I'm closing the browser) or window.open('','_self','');window.close() (horrible, I agree but it's the only way I found to avoid the JS prompt)
On firefox it works perfectly but on IE7 (the browser our customers have) after 2-3 times I use that button to close the window I can't open other windows (in both cases, with or without the JS prompt). With the method above it does nothing, and with a click me a new window is opened but hangs on loading (it doesn't even calls the Page_Load).
What could be the cause? How can I solve this?
Thank you.
EDIT:
I forgot to mention that I'm using MS Ajax in most of the pages, and that may be the reason that forces me to use window.open('','_self',''); before window.close()
I don't know if this could cause also the hanging of IE
EDIT: Ignore that, it does still prompt the user - sorry!
For your first issue about closing the window, have you tried:
self.close();
Not too sure about the hanging issue though, I use window.open() and have never experienced issues in IE7.
I finally came to a solution:
on the attribute assignment there was a return false; missing.
Now it works perfectly with "window.open('','_self','');window.close();return false;".

How do I POST to a web page using Firebug?

How do I POST to a web page using Firebug?
You can send POST request to any page by opening console (e.g. in FireFox ctrl + shift + k) and typing simple JS:
var formPost = document.createElement('form');
formPost.method = 'POST';
formPost.action = 'https://www.google.com'; //or any location you want
document.body.appendChild(formPost);
formPost.submit();
AFAIK Firebug can't do this. However, there is a very useful Firefox extension, in the spirit of Firebug, called Tamper Data. This should be able to do what you want.
It allows you to monitor each request made by the browser, and you can turn on an option that allows you to look at, and edit, every single request before it gets sent.
Firefox 27 (maybe earlier versions too, never checked) has built-in developer tools to modify and resend requests. If you don't have Firebug installed, the console is available by pressing the F12 key. If Firebug is installed, press Ctrl+Shift+K instead.
I know this is an old question, but I recently stumbled upon the same problem and wanted to share the method I am using.
Assuming the web site you want to POST to has a form with method="POST" (a very likely scenario), you can use Firebug's JavaScript command line to programmatically submit a POST request. Just click the "Show Command Line" icon in Firebug and enter something like this in the narrow text box at the very bottom of the window:
document.forms[0].submit()
Maybe this helps someone.
Another simple solution is to load any webpage that uses jQuery, and type up a $.post() in the console.
HTTP resource test is a firefox plugin that can do this.
Another powerful Firefox plugin to perform post request and some more features is the Hackbar.
Related:
To resend a POST already made, right click the POST request in the Net/XHR view and click "Resend".
Using Firebug 1.12.0:
Got here looking for a Firebug way of doing this. Then I realized that I could use Fiddler. This is the most powerful tool I know when it comes to debugging web requests.
Fiddler The free web debugging proxy for any browser, system or
platform
Click the Composer tab and write your request as desired - then click Execute.
NO NEED of plugins !!
Just drag any url in BOOKMARK BAR, then right click and EDIT, and insert javascript code:
javascript:var my_params=prompt("Enter your parameters","var1=aaaa&var2=bbbbb"); var Target_LINK=prompt("Enter destination", location.href); function post(path, params) { var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", path); for(var key in params) { if(params.hasOwnProperty(key)) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("name", key); hiddenField.setAttribute("value", params[key]); form.appendChild(hiddenField); } } document.body.appendChild(form); form.submit(); } parsed_params={}; my_params.substr(1).split("&").forEach(function(item) {var s = item.split("="), k=s[0], v=s[1]; parsed_params[k] = v;}); post(Target_LINK, parsed_params); void(0);
then enter the target site-link, and click that button in BOOKMARK BAR! That's all!
( source: https://stackoverflow.com/a/38643171/2377343 )

Resources