Asp.net Display Pdf on Chrome - Error ERR_HTTP2_PROTOCOL_ERROR - asp.net

I want to allow users to display a pdf and save it on my asp.net project.
My code is below:
string yol = e.CommandArgument.ToString();
string path = Server.MapPath("~/Raporlar/2021/" + yol.Trim());
WebClient User = new WebClient();
Byte[] s = User.DownloadData(path);
System.IO.MemoryStream ms = new System.IO.MemoryStream(s);
if (ms != null && ms.Length > 1)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = "UTF-8";
Response.Buffer = true;
Response.AddHeader("Content-Length", ms.Length.ToString());
Response.AddHeader("Content-Disposition", "inline; filename=\"" + yol + "\"");
Response.AddHeader("Expires", "0");
Response.AddHeader("Pragma", "cache");
Response.AddHeader("Cache - Control", "private");
Response.ContentType = "application/pdf";
Response.BinaryWrite(ms.ToArray());
Response.Flush();
try { Response.End();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
}
catch { }
}
The display button only works Firefox. Other browsers (Google Chrome, Opera, Edge..) is not display the pdf and gives error like that:
Hmmm… This page is not available. The web page at https://massgrup.com/Raporlar.aspx may be experiencing some problems or has been permanently moved to a new web address.
ERR_HTTP2_PROTOCOL_ERROR

I solved the problem. It is about ssl certifica.
When i click the button the link is like this : "https:https://massgrup.com/"
Because of that the button is not working on Chrome browsers.
After removing the ssl certificate the problem is resolved.
But now I will have to solve the problem of how to install the ssl certificate. This is a different situation.

Related

ASP.NET Web Forms Download String as File without interfering with initial request

In my ASP.NET Web Forms application, I have to download a string as a file right after i'm doing an action on the web page. The flow is as follows:
Do initial action (button click)
It opens up a popup window where the username and password are required.
Fill in the username and password
Do second action (button click inside popup)
Now, after the second action is done, I want my page to reload and start downloading my string as a file.
If I write my string in the response like below, the file gets downloaded, but the pop-up window does not close.
Response.Write(mystring);
I tried these ways:
Context.Response.Clear();
Response.AddHeader("Content-Disposition", "inline; filename=Warrants.warrant");
Response.ContentType = "application/lsw_print";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(mystring);
Response.Flush();
Response.End();
2.
Context.Response.Clear();
Response.AddHeader("Content-Disposition", "inline; filename=Warrants.warrant");
Response.ContentType = "application/lsw_print";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(mystring);
Response.Flush();
ApplicationInstance.CompleteRequest();
Context.Response.Clear();
Response.AddHeader("Content-Disposition", "inline; filename=Warrants.warrant");
Response.ContentType = "application/lsw_print";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(mystring);
All these versions download my file, but ends the requests and my pop-up doesn't get closed.
Also, the popup I use is a control having an iframe in it.
Thanks!

ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION - unable to find out why

I'm getting ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION when returning my docx file, however I'm not sure where in my header the error comes from. I've tried my best to remove anything that is dynamic and hardcoded to figure it out, to no avail.
byte[] byteArray = File.ReadAllBytes(#"T:\Praktik log\Learning Goals.docx");
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray,0,byteArray.Length);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true))
{
RevisionAccepter.AcceptRevisions(wordDoc);
wordDoc.MainDocumentPart.Document.Body.InsertAt(
new Paragraph(
new Run(
new Text("Newly inserted paragraph."))), 0);
}
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
//Add the header & other information
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl = "private";
Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AppendHeader("Content-Length", mem.ToArray().Length.ToString());
Response.AppendHeader("Pragma", "cache");
Response.AppendHeader("Expires", "60");
Response.AppendHeader("Content-Disposition",
"attachment; " +
"filename=\"ActivationKey.docx\"; " +
"size=" + mem.ToArray().Length + "; " +
"creation-date=" + DateTime.Now.ToString("R") + "; " +
"modification-date=" + DateTime.Now.ToString("R") + "; " +
"read-date=" + DateTime.Now.ToString("R"));
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
//Write it back to the client
Response.BinaryWrite(mem.ToArray());
Response.End();
I can download it on IE as it's gives crap about security, but chrome says nogo, and I need to have Chrome support for this download.
EDIT: Fiddler response: https://prnt.sc/pddibg
You're doing one weird thing.
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
ASP.NET places some headers itself.
Please try to not remove them and use native (if they exist for headers you're trying to modify, of course) ASP.NET methods to modify response headers.
Also you can setup a HTTP debugging proxy.
It will help you see what exactly you are doing wrong.
Here's some HTTP debugging proxies I know: Fiddler, Wireshark or Charles (paid, requires Java).
This guy achieved file downloading via returning System.IO.File.
Also this question looks like yours.
Check these answers, they may help you.
I achieved downloading a named file via:
[HttpGet]
public void Get()
{
Response.Headers.Add("Content-Disposition", "attachment; filename=\"file.bin\"");
var buffer = Enumerable.Repeat((byte) 0xF, 2048).ToArray(); //bytes
Response.Body.Write(buffer, 0, buffer.Length);
}
Tested on Google Chrome (Version 77.0.3865.90).
File downloaded and contains 2048 bytes of data.
Server response in Fiddler: screenshot

Failed - network error in get Zip file in chrome

I am creating a zip by Ionic.Zip.dll like this (ASP.NET,C#):
zip.AddEntry("Document.jpeg", File.ReadAllBytes("Path");
I want to download it like this:
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=SuppliersDocuments.zip";
zip.Save(Response.OutputStream);
Response.Close();
I tested this code in localhost by Firefox and Chrome and it worked properly. But when I test this code in host, I get this error:
Failed - network error
Is my code is wrong?
I ran into a similar issue with relaying an SSRS report. Taking #Arvin's suggestion, I did the following:
private void CreateReport(string ReportFormat)
{
ReportViewer rview = new ReportViewer();
// (setup report viewer object)
// Run report
byte[] bytes = rview.ServerReport.Render(ReportFormat, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
// Manually create a response
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}.{1}", fileName, extension));
// Ensure the content size is set correctly
Response.AddHeader("Content-Length", bytes.Length.ToString()); // <- important
// Write to the response body
Response.OutputStream.Write(bytes, 0, bytes.Length);
// (cleanup streams)
}
The fix was adding the Content-Length header and setting it to the size of the byte array from reporting services.

Download Button to download from picture library

I have a ListView with a picture library as source and for each picture I have a asp:LinkButton that represents a download button. When pressed my button should open the browser download box. I'm using the following code to achieve that:
public void Download_Click(object source, EventArgs e)
{
LinkButton button = (LinkButton)source;
string url = Server.UrlEncode(button.CommandArgument);
FileInfo fileInfo = new FileInfo(url);
if (fileInfo.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.WriteFile(fileInfo.FullName);
}
else
{
//error
}
}
On that code I'm using Server.Encode("fileName") but I tried Server.Map and also used the "PicLibraryName/FileName" and "Application/PictureLibraryName/FileName" but I never get FileInfo.Exists true because I always get FileNotFoundException or System.Web.HttpException (when I use a virtual path).
Anyone knows what's the best way to solve this problem?
I think you should use WebClient.DownloadData(url) for downloading the file and then Response.OutputStream.Write method for output downloading data, also don't forget to end response using Response.End() something like this:
var wc=new System.Net.WebClient();
var data=wc.DownloadData(url);
Response.Clear();
//add headers..
Response.OutputStream.Write(data);
Response.End();

exporting to excel over https(SSL) using IE is not working in asp.net website

i am trying to export an excel file from an https site, it is working great with
firefox and other browsers but in IE its is giving me error that the site is not available or the file is not found.
i have research the problem and i have found it is cache related so i have change the headers in this page so it will allow caching but it is still not working.
this is a snap of the code
Response.Cache.SetAllowResponseInBrowserHistory(true);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0));
Response.Cache.SetAllowResponseInBrowserHistory(true);
Response.CacheControl = "Public";
any help would be great thank you
#region For Download to work on HTTPS
Response.ClearHeaders();
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.Buffer = true;
Response.AddHeader("Content-Transfer-Encoding", "binary");
#endregion
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Temp.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter strWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(strWriter);
string heading = "<h4 align='left'>Temp Excel Cube</h4>";
strWriter.Write(heading);
gvTemp.RenderControl(htmlWrite);
Response.Write(strWriter.ToString());
Response.End();
I guess this is what you are looking for. It works for me on HTTPS, I've exported a grid to an excel sheet, you can use it to pickup a file from a physical location too. :)

Resources