Open PDF new browser tab ASP.net 2.0 - asp.net

I am trying to open a PDF in a new tab in the browser. So far I have been able to get the PDF in the browser within the "same" page. However I would now like it to open it in a new tab. However I am stuck on how to achieve this?
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileId & fileExtension)
Response.AddHeader("Content-Length", bytesReturned.Length.ToString())
Response.OutputStream.Write(bytesReturned, 0, bytesReturned.Length)
Response.Flush()
Response.End()
Result:
The result is displayed in a gridview, I would like to link each "view" button to a target"_blank" attribute. The "ID" corresponds to the file name.
From my research, I can see you can add a button in the aspx page and give it target "_blank" attribute but I am not sure how to link it to my displayPDF() method?
If the question is unclear, please let me know.

One way is to make view button an anchor and set its attribute to _blank and second is to open file by using code behind.
Call this code on button click
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "window.open('"+strFileId & fileExtension+"','_blank');", true);

Related

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.

Generating pdf before leaving a page in Asp.net

After submitting a form and before Redirecting a page need to generate a pdf, ask the user to save the file, then redirect the page .
Below is my code
DataSet objData = objInsert.Execute();
DocLib dl = new DocLib();
System.IO.MemoryStream ms = dl.GenerateForm(objData.Tables[0].Rows[0]);
Response.AddHeader("content-disposition", "attachment;filename=" +OrderNo + ".pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Redirect("OrderList.aspx");
But currently once the save dialog box appears the page stops navigation and remains in the same page. How to redirect to another page.
I can't see a reason to effect Redirect's work. As another solution, you can try this.
Page.ClientScript.RegisterStartupScript(typeof(Page),"navigation",
"<script>window.location.href='OrderList.aspx'</script>")

download xml file using asp.net

I have created a xml file using c# and I can save it at any location in my machine (in this situation I have saved it on the root of the application with name "temp.xml"), but I want to let it for the user to download it from their browser buy giving a link like-->
"click HERE to download the file."
In Chrome and FireFox it show a new tab with only some values in my body part of the xml file but IE shows the whole xml. I want to download it in my download folder when anybody click on the above link.
thanks in advance for your support.
You need to specifically mention the file type and name. and use the TransmitFile method. This will show the Save As window.
I got the following code from this webpage
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg");
Response.TransmitFile( Server.MapPath("~/images/sailbig.jpg") );
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.AddHeader("content-disposition", "attachment;filename=temp.xml");
Response.ContentType = "text/xml";
Response.Write(File.ReadAllText(Server.MapPath("~/temp.xml"))); //you may want to write the XML directly to output stream instead of creating an XML file first.
Response.End();
Hope this helps.

ASP.NET 4: HttpResponse open in NEW Browser?

I am using a PDF generater that utlizes HttpResponse. Is there a way (perhaps passing a header tag) to open the PDF in a NEW windows instead of the same one? I don't want the user to be directed away from the website...
Here's the code I'm using:
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear();
response.AddHeader("Content-Type", "application/pdf");
response.AddHeader("Content-Disposition",
"inline; filename=" + downloadName + "; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush();
response.End();
You cannot do that on server side if you have not generated the link yourself. But if you have, then as Robert said, provide a target with the <a> link.
There is a server-side alternative and that is to set the content type to application/octect-stream so that the file is download and user will be able to open it with the application of choice outside browser. See here for more.
You also need to use content disposition header to provide the file name so that client can know what file type is it after it has been downloaded as binary.
Content-Disposition: attachment; filename=my.pdf;
You presumably have a link on your site that directs the user to the URL where this PDF generation is done. In that link, add target="_blank" to the <a> tag.
If what you're really asking is how to make the save dialog to show instead of having the PDF loaded into your browser, then you need to modify your header.
You are setting Content-Disposition to 'inline', change that to 'attachment'
http://support.microsoft.com/kb/260519

File download does not work with IE7

I have an asp.net page which sends content of a file to the client, so the browser shows the save as dialog to download the file. This page is displayed in a popup and when the user clicks the save button, it closes automatically and the download starts.
On windows server 2003, it works fine. On vista with other browsers, also works fine. But when I try with IE7 & Vista, the popup opens, and closes after about a second without displaying the file download dialog. How can I solve this?
The code I use for response generation is:
FileStream fileStream = new FileStream(filePath, FileMode.Open);
int fileSize = (int)fileStream.Length;
byte[] buffer = new byte[fileSize];
fileStream.Read(buffer, 0, (int)fileSize);
fileStream.Close();
Response.Clear();
Response.Buffer = true;
Response.BufferOutput = true;
Response.ContentType = "application / octet - stream";
Response.AddHeader("Content-Length", buffer.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.AddHeader("Extension", Path.GetExtension(filename));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
And I am opening the popup with this javascript:
window.open ('Download.aspx?filename=somefile.ext','downloadWindow','location=0,status=0,scrollbars=0,width=1,height=1');
EDIT: I corrected the spaces but unfortunately they are not the problem.
EDIT 2:: Seems that this problem is not related to Vista but IE only. I also discovered that it works fine when the project is run on the development server locally but when working as connected to publish server, it fails to download the file.
Try removing the spaces in your ContentType. The standard is application/octet-stream.
Two things.
As mentioned before you will want to remove the spaces in the type
Is there any particular reason that you are not using Response.TransmitFile() rather than reading the file in yourself?
I'd also suggest you add quotes around the file name, otherwise, if it contains spaces, it will get truncated in Firefox.
I can't point to a specific problem in your code (except possibly for that content type, which looks badly-formed; not sure if that makes a difference). Here's the code I use for this, which works in both IE7 and Firefox:
Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.CacheControl = "public";
Response.OutputStream.Write(byteArr, 0, byteArr.Length);
Response.End();
I think your problem could be with IIS 7 . There is a problem with "addHeader" in the new Internet information Server with the integration pipeline mode.
Try to use Response.AppendHeader .
I also had the same problem...and I used this solution (I'm using it on a button.click):
Response.ContentType = "text/txt";
Response.AppendHeader("Content-Disposition", "attachment; filename="+DownloadFileName);
Response.Write(MyFileContent_Text_);
Response.End();
...it just worked!!
I came across this post because I was having a similar problem if not the same one. I am running IE8 on Windows 7.
When debugging on my local machine I could get the File Download prompt to display, but when clicking "Save" or "Open" the Download Progress window would display for about a half second and then close suddenly without downloading anything.
I have an add-on installed for Internet Explorer called IE7Pro. It comes with a Download Manager which I had enabled. When I disabled it, my problems went away and I could Open or Save my files.
Hope this proves helpful to someone else out there.

Resources