Download Files When Webpage is Inside an iFrame - c#

I have a Master page with an iFrame.
Tickets.aspx has this Master. General.aspx is without any Master and loads inside the iFrame.
Both the pages have buttons to download files.
When downloading file from General.aspx, it shows this error.
Unable to evaluate expression because the code is optimized or a
native frame is on top of the call stack.
General.aspx.cs:
protected void View (object sender, CommandEventArgs e)
{
string sFile = "~/Attachments/"+(e.CommandArgument.ToString());
if(File.Exists(Server.MapPath(sFile)))
{
Response.Redirect("/Forms/DownloadFile.aspx?file="+sFile); //ERROR HERE
}
}
DownloadFile.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string sPath = Server.MapPath(Request.QueryString["file"]);
FileInfo file = new FileInfo();
if(file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition","attachment; filename="+file.Name);
Response.AddHeader("Content-Length",file.Length.ToString());
Response.ContentType="application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
}
DownloadFile.aspx uses the same Master and has the code. There is no problem when downloading from Tickets.aspx. I thought this could be due to the iframe. So I created a similar download page without a master. But still the same error.
How can I resolve this.

Related

Changing label text on button click that also executes other methods

I am trying to update a label on my windows form with statistics from another methods execution that scrapes a webpage and creates a zip file of the links and creates a sitemap page. Preferably this button would run the scraping operations and report the statistics properly. Currently the scraping process is working fine but the label I want to update with statistics data is not changing on the button click. Here is what my code looks like now:
protected void btn_click(object sender, EventArgs e)
{
//Run scrape work
scrape_work(sender, e);
//Run statistics work
statistics(sender, e);
}
protected void scrape_work(object sender, EventArgs e)
{
//Scraping work (works fine)
}
protected void statistics(object sender, EventArgs e)
{
int count = 0;
if (scriptBox.Text != null)
{
count += 1;
}
var extra = eventsBox.Text;
var extraArray = extra.Split('\n');
foreach (var item in extraArray)
{
count += 1;
}
//scrapeNumLbl is label I want to display text on
scrapeNumLbl.Text = count.ToString();
}
Would I have to use threading for this process or is there some other way I can get this process to work? I have already tried this solution but was having the same issue where the code runs but the label does not update. Any help would be greatly appreciated, this minor thing has been bugging me for hours now.
I eventually solved this by writing the path to the zip file to a label button on the form rather than sending it right to download on the client's browser on button click. The issue was that the request was ending after the zip file was sent for download. To ensure that both methods ran at the proper time I moved the call to the scrape_work method to inside of of statistics
In order for the path to be clickable and the file to download properly I had to make the "label" in the form a LinkButton in the .aspx page
<asp:LinkButton ID="lblDownload" runat="server" CssClass="xclass" OnClick="lblDownload_Click"></asp:LinkButton>
And made the lblDownload_Click run like the following:
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=zipFiles.zip");
string folder = Server.MapPath("~/zip");
string endPath = folder + "/zipFiles.zip";
Response.TransmitFile(endPath);
Response.End();
Running it this way the page reloads with the new labels properly written and the zip file available to download as well.
ASSUMING THIS CODE IS RUNNING SYNCHRONOUSLY (you aren't threading the scraping in a call I don't see), Are you sure you are reaching the code that sets the label text? I simplified your code as below (removing the iteration over the array and just setting the label text to a stringified integer) and am not having any trouble changing the text of a label.
namespace SimpleTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
scrape_work(sender, e);
statistics(sender, e);
}
protected void scrape_work(object sender, EventArgs e)
{
//Scraping work (works fine)
}
protected void statistics(object sender, EventArgs e)
{
int count = 666;
scrapeNumLbl.Text = count.ToString();
}
}
}
Result:

Response Redirect not working

private void Button1_OnClick(object sender, EventeArgs e)
{
Response.Redirect(myselect.SelectedValue.ToString(), true);
}
Above is my code, I already put a breakpoint on .SelectedValue and it's recognizing the value, but when I click on the button it shows this message:
to do what you need, you must download the file to the client. Response.Redirect as people mentioned redirects to URL.
To make it open in the browser you need the below :
private void Button1_OnClick(object sender, EventeArgs e)
{
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline; filename=MyFile.pdf");
Response.TransmitFile(myselect.SelectedValue.ToString());
Response.End();
}
For Content-Disposition you have two choices :
Response.AppendHeader("Content-Disposition", "attachment;filename=somefile.ext") : Prompt will appear for file download
Response.AppendHeader("Content-Disposition", "inline;filename=somefile.ext") : the browser will try to open the file within the browser.
Your sample is assuming that a site e.g. 1.aspx or 221.aspx exists. You are only passing some selected value.
private void Button1_OnClick(object sender, EventeArgs e)
{
Response.Redirect(myselect.SelectedValue.ToString(), true);
}
you need to redirect to some kind of action like:
public FileResult DownloadFile(int id) {
// Your code to retrieve a byte array of your file
var thefileAsByteArray = .....
return File(thefileAsByteArray, System.Net.Mime.MediaTypeNames.Application.Octet, 'DownloadFilenName.pdf');
}
Then you would need to change your onClick metho like:
private void Button1_OnClick(object sender, EventeArgs e)
{
Response.Redirect("Download.aspx?id=" + myselect.SelectedValue.ToString(), true);
}

asp.net Load Child page only from parent page

I have two pages MainPage.aspx and ChildPage.aspx. From main page when i click a button i redirect to ChildPage.
If i give the address of ChildPage directly on browser, i do not want to load it directly instead i want to redirect to MainPage.
the ChildPage must be loaded only if it is loaded from the MainPage.
How do I find from where the ChildPage.aspx is loaded. how to find the parent page of it or from where it is loaded.
can we try something in the below code
if (!IsPostBack)
{
if (finding_source)
{
Response.Redirect("MainPage.aspx");
}
}
You can use Request.UrlReferrer.AbsolutePath to see the previous page.
if (!IsPostBack)
{
if (Request.UrlReferrer != null && Request.UrlReferrer.AbsolutePath == "/MainPage")
{
//do what you want
}else{
Response.Redirect("~/MainPage.aspx");
}
}
TIP But be careful with using it with postbacks since it will change the value of Request.UrlReferrer to the current page during a postback.
Even though your question is not clear , here i am trying to give my solution.
MAINPAGE.ASPX BUtton Click
protected void lnkRegister_Click(object sender, EventArgs e)
{
Session["MainPage"] = "true";//Encrypt it if u wish
Response.Redirect("childpage.aspx");
}
CHILDPAGE.ASPX PAGE LOAD
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Session["MainPage"] as string) && Session["MainPage"].Tostring()=="true")
{
//proceed
}
else
{
Response.Redirect("mainpage.aspx");
}
}
GLOBAL.ASAX
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
Session.RemoveAll();
Session.Clear();
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}
it is little difficult to control browser shutdown or close...but you can workaround global asax file when your application shutsdown.

Exporting Data from Telerik RadGrid to Ms-Word

I'm trying to export a Telerik RadGrid to Word:
protected void DownloadWord_Click(object sender, EventArgs e)
{
aGrid.ExportSettings.Word.Format = GridWordExportFormat.Docx;
aGrid.ExportSettings.ExportOnlyData = true;
aGrid.ExportSettings.FileName = "test.docx";
aGrid.MasterTableView.ExportToWord();
Page.Response.ClearContent();
Page.Response.ClearHeaders();
}
When I click the button, the above code fires and I get a page refresh. Looking at the POST headers and response it is the same as for loading the page, so it seems to only refresh the page.
What's wrong?
The RadGrid writes the exported file into the response of the page. And since you are clearing the response and its headers, you get no file.
For your case, Only this is required:
protected void ImageButton3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
aGrid.ExportSettings.Word.Format = Web.UI.GridWordExportFormat.Docx;
aGrid.ExportSettings.FileName = "test.docx";
aGrid.ExportSettings.ExportOnlyData = true;
aGrid.MasterTableView.ExportToWord();
}
I cant understand the use of:
Page.Response.ClearContent();
Page.Response.ClearHeaders();
Unless you have added it for some specific purpose.
See here for demo from telerik on using Export functionalities.

Getting the HTML content of the current page with databound controls

I'm trying to create a control that converts the page it's on into a PDF.
protected void ConvertPageToPDF_click(object sender, EventArgs e)
{
string pageHtml;
byte[] pdfBytes;
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// get the HTML for the entire page into pageHtml
HtmlTextWriter hw = new HtmlTextWriter(new StringWriter());
this.Page.RenderControl(hw);
pageHtml = hw.InnerWriter.ToString();
// send pageHtml to a library for conversion
// send the PDF to the user
}
This partially works. On my pages I have several repeaters; their content does not show up in pageHtml. Any thoughts on why that is? How should I fix this?
It turns out that I had to render the control after the data had been bound to it. My click method just adds an event handler:
protected void ConvertRepeaterToPDF_click(object sender, EventArgs e)
{
// handle the PreRender complete method
Page.PreRenderComplete += new EventHandler(Page_OnPreRenderComplete);
}
Then in Page_OnPreRenderComplete I can use RenderControl as above.

Categories