Open HttpResponse in a new tab - c#

I have the following case.
I am creating a filestream from a pdf file.
after the reponse the generated PDF will be opened in the same tab to print, save etc.
The question is: HOW DO I CHANGE THIS CODE SO IT WILL OPEN IN A NEW TAB (OR WINDOW)?
string filepath = String.Format(*Path to file*);
string filename = *Filename*;
try
{
FileStream fs = new FileStream(filepath, FileMode.Open);
MemoryStream ms = new MemoryStream();
fs.CopyTo(ms);
var docLength = fs.Length;
fs.Close();
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.BufferOutput = true;
Response.ContentType = "Application/pdf";
response.AddHeader("content-disposition", "inline; filename=" + filename);
response.AddHeader("Content-Length", docLength.ToString());
//response.Write("<script>");
//response.Write("window.open('" + filepath + "',_newtab');");
//response.Write("</script>");
byte[] data = req.DownloadData(filepath);
response.BinaryWrite(data);
}
catch (Exception ex)
{
// Here I log the exception to a text file.
}
finally
{
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
}
I have tried several options, but still not found a solution that works for me.
Can someone help me with this.
This function is not directly triggered by a button.
the button triggers another method which generates some other stuff and after that it triggers the method described above.

Response.Write("<script>");
Response.Write("window.open('../LOCATION/pages/FILENAME.pdf', '_newtab');");
Response.Write("</script>");

Related

Why is my file downloading only when I click retry .net core

I'm trying to export data from database to an excel file and then download that file on client side. The export works fine but the problem is with the download part. Every time I call the method "DownloadCurrent" I get failed attempt in the browser. But if I click on retry (circle arrow) in browser then it downloads the file with no problem.
Code :
...
string fileName = className + "Export.xlsx";
string filePath = Directory.GetCurrentDirectory() + "\\wwwroot\\" + fileName;
public void DownloadCurrent(String filePath, String fileName)
{
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(filePath);
if (buffer != null && buffer.Length > 0)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Headers.Add("content-length", buffer.Length.ToString());
Response.Headers.Add("content-disposition", "attachment; filename=" + fileName);
Response.Body.Write(buffer);
}
}
I'm using VS 2022, and .NET 6.0
ASP.NET Core Web App
As stated in a comment above, don't use WebClient anymore.
However, based on your code, you don't need to use either WebClient or HttpClient. It appears your file is a local file that you simply need to stream to the browser. The only reason I see that you would need to use a WebClient or HttpClient for this, is if that file needs to be first downloaded from a remote server via a URI, for example.
Something like this code should do the trick:
public void DownloadCurrent(String filePath, String fileName)
{
using FileStream fs = File.OpenRead(filePath);
long length = fs.Length;
byte[] buffer = new byte[length];
fs.Read(buffer, 0, (int)length);
Response.ContentType = "application/vnd.ms-excel";
Response.Headers.Add("content-length", length.ToString());
Response.Headers.Add("content-disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(buffer);
}
For more, please see: https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.binarywrite?view=netframework-4.8.

Display .jpg / .png image from folder in browser instead of downloading it ASP.NET

I am trying to display .png / .jpg image in browser but the problem is instead of displaying the image in the browser it is getting downloaded. I have tried using content-disposition:inline as well but its downloading the complete aspx page. Can anyone help me with this. Below is my code
string filePath = "C:\\inetpub\\wwwroot\\Folder\\OSP\\20139000\\";
string filename = "test.jpg";
string contenttype = "image/" +
Path.GetExtension(filename.Replace(".", ""));
FileStream fs = new FileStream(filePath + filename,
FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
////Write the file to response Stream
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contenttype;
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
You miss
Response.Clear();
somewhere at the beginning of the script. Without clearing the response's buffer first, it already contains the text of the *.aspx page the script is run under.
Then, don't set the content's disposition to attachment, completely delete this line.

FIle Downloading asp.net

I am trying to download a file using a button on asp.net, but the button gives me the webform aspx as the the downloaded file in my case DownloadFileTest.apsx. I needed to download the right file. This might help I the uploaded file in my solution explorer doesn't show up either. But it shows up if I access it inside the folder of my project. Here is the code
protected void Button1_Click(object sender, EventArgs e)
{
string filename = TextBox1.Text;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-diposition", "attach;filename" + filename);
Response.TransmitFile(Server.MapPath("~/CustomerFiles/" + filename));
Response.End();
}
This is the code I use to download file, make sure fuldFilNavn contains the full path of the file:
public static void DownloadFil(string fuldFilNavn)
{
HttpContext context = HttpContext.Current;
context.Response.ClearHeaders();
context.Response.ClearContent();
string filNavn = Uri.EscapeDataString(Path.GetFileName(fuldFilNavn)).Replace("+", "%20");
context.Response.AppendHeader("Content-Disposition", "attachment;filename*=utf-8''" + filNavn);
context.Response.AppendHeader("Last-Modified", File.GetLastWriteTimeUtc(fuldFilNavn).ToString("R"));
context.Response.ContentType = "application/octet-stream";
context.Response.AppendHeader("Content-Length", new FileInfo(fuldFilNavn).Length.ToString());
context.Response.TransmitFile(fuldFilNavn);
context.Response.End();
}
This will download files with unicode characters in the filename!
You may try the following ASP.NET/C# code snippet:
internal static void Download(string FileName)
{
HttpResponse _response = HttpContext.Current.Response;
FileStream _fileStream;
byte[] _arrContentBytes;
try
{
// clear response obj
_response.Clear();
// clear content of response obj
_response.ClearContent();
// clear response headers
_response.ClearHeaders();
// enable response buffer
_response.Buffer = true;
// specify response content
_response.ContentType = ContentType;
_response.StatusCode = 206;
_response.StatusDescription = "Partial Content";
// create FileStream: IMPORTANT - specify FileAccess.Read
_fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// Bytes array size= (int)_fs.Length;
_arrContentBytes = new byte[(int)_fileStream.Length];
// read file into bytes array
_fileStream.Read(_arrContentBytes, 0, (int)_fileStream.Length);
// add response header
_response.AddHeader("content-disposition", "attachment;filename=" + FileName);
// ACTUAL PROCEDURE: use BinaryWrite to download file
_response.BinaryWrite(_arrContentBytes);
// ALTERNATIVE: TransmitFile
//_response.TransmitFile(filePath);
// close FileStream
_fileStream.Flush();
_fileStream.Close();
_response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch { }
finally
{
_fileStream = null;
_arrContentBytes = null;
}
}
In order to get the root folder and full path you may use Server.MapPath as in your original solution or the following line for better performance:
// get the root dir; fast
string _root = AppDomain.CurrentDomain.BaseDirectory;
This solution has been tested/implemented in the actual web app (http://taxiom.com/Manual_Payday.aspx) - refer to the "Download" button in the upper-right corner of the page for the demo. Hope this may help.

ContentType application/pdf doesn't work in safari

From handler I attach in response pdf document in memoryStream:
context.Response.Clear();
MemoryStream ms = new MemoryStream(pdfByte);
context.Response.AddHeader("content-disposition",
"attachment;filename=myDoc.pdf");
context.Response.ContentType = "application/pdf";
context.Response.Buffer = true;
ms.WriteTo(context.Response.OutputStream);
context.Response.End();
In all browser work perfect, but in safari downloaded document has this name format=> myDoc.pdf.txt
Some proposal how solve?
This is what I typically use for showing PDF files in a browser client:
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
var data = new byte[(int)stream.Length];
stream.Read(data, 0, System.Convert.ToInt32(stream.Length));
var cd = new System.Net.Mime.ContentDisposition
{
FileName = yourfilename,
Inline = false,
};
Response.AddHeader("Content-Disposition", cd.ToString());
This way is the only reliable way I could find to ensure all browser perform consistently. You should be able to use the header infor with Memory Stream

Recreating a new PDF still holds the old Image, using ASP.NET, C# and Winnovative.WnvHtmlConvert.PdfConverter

If I build a PDF (pdf1) with an image(image1), pdf1 shows image1 as expected.
If I then replace image1 with image2, in the site, and make a new pdf2, pdf2 shows the old image1, and that's my (caching?) problem.
For more information:
If I stop my program in VS and close all my Development Servers (Local ISS?), run the program again and make a new pdf (pdf3), the pdf3 shows the image2(the last image i made), which is correct.
So I guess i dont end some things or cache to much?
How I create the PDF
public void CreateSingleFrontpage(string url)
{
var pdfConverter = new PdfConverter(0);
PdfConverter.LayoutHtmlTimeoutSec = 500;
pdfConverter.NavigationTimeout = 5000;
pdfConverter.LicenseKey = "****************************";
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
pdfConverter.PdfDocumentOptions.PdfCompressionLevel =
PdfCompressionLevel.Normal;
pdfConverter.PdfDocumentOptions.PdfPageOrientation =
PDFPageOrientation.Portrait;
pdfConverter.PdfDocumentOptions.ShowHeader = false;
pdfConverter.PdfDocumentOptions.ShowFooter = false;
pdfConverter.PdfDocumentOptions.LeftMargin = 80;
pdfConverter.PdfDocumentOptions.RightMargin = 40;
byte[] pdfBytes = pdfConverter.GetPdfBytesFromUrl(url);
// send the PDF document as a response to the browser for download
System.Web.HttpResponse response =
System.Web.HttpContext.Current.Response;
response.Clear();
// response.CacheControl = "no-cache";
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition", "attachment;
filename=PDF_Temp.pdf; size=" + pdfBytes.Length);
response.Flush();
response.BinaryWrite(pdfBytes);
response.Flush();
response.End();
}
ProcessRequest
public void ProcessRequest(HttpContext context)
{
int skemaId = int.Parse((context.Request.QueryString["SkemaId"]));
int witchImage = int.Parse(context.Request.QueryString["witchImage"]);
byte[] imageData = new BLL.Handlers.PDFForsideHandlers().GetImage(
witchImage, skemaId);
if (imageData != null)
{
context.Response.ContentType = "image/jpg";
context.Response.BinaryWrite(imageData);
// context.Response.Flush();
// context.Response.Clear();
// context.Response.Close();
// context.Response.End();
}
}
asp.net control
Image image = new Image();
image.ImageUrl = url;
image.DataBind();
PlaceHolder1.Controls.Add(image);
I spend a whole day on this now, any comments would be much appriciated.
Winnovative is grabbing the image from the cached under C:\Windows\Temp\Temporary Internet Files\Content.IE5 for IE9. This "windows" ie cache directory is different based on your version of IE.
It turnes out that it worked on another computer. So it must be my FF and Chrome settings or the function the browser has to handles PDF's that's no good. If anyone else care..

Categories