ASP.NET 4 downloaded files in CHROME IOS corrupted - asp.net

the problem occurs only with chrome/IOS. downloading on safari/ios, chrome/windows, chrome/mac works fine. all downloaded files from our site are corrupted(zip and pdf) when using chrome/ios. Also Chrome and ios is on latest version. How can we fix it?
private static HttpResponse NewHttpResponse_OpenFile(string fileName)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ContentType = "application/octet-stream";
System.Web.HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser;
if (browser.Browser.Equals("IE") || browser.Browser.Equals("InternetExplorer"))
{
response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileNameWithoutExtension(fileName)) + System.IO.Path.GetExtension(fileName));
}
else
{
response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
}
return response;
}

Related

Google Chrome Not Display PDF from asp.net Page

I want to display a pdf file on an .aspx page with a button. The codes is like that:
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 codes works on Firefox, Edge. But it is not work on Google Chrome. What could be problem ? Can you help me ?

'http:/***.168.**.8:***/UploadedFiles/CustomerKYC/Photo/134_26581.jpg' is not a valid virtual path

I am getting the exception http:/.168.11.8:/UploadedFiles/CustomerKYC/Photo/134_26581.jpg' is not a valid virtual path when I write either WriteFile or TranferFile in the following code. Please give me a code fix.
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "filename=" +NavigateURLID.Value);
Response.WriteFile(Server.MapPath(url));
Response.Flush();
Response.End();
I need the file from the url to be downloaded. They are all image files only(jpg)
Try this code will help
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
Reference url: https://www.aspsnippets.com/Articles/Upload-and-Download-files-from-Folder-Directory-in-ASPNet-using-C-and-VBNet.aspx
I found out the answer after searching in Internet. The code is.
WebClient req=new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer= true;
response.AddHeader("Content-Disposition","attachment;filename=\"" +strURL + "\"");
byte[] data=req.DownloadData(strURL);
response.BinaryWrite(data);
response.End();

Export to excel using Response.TransmitFile() is not working properly

I have written some content to an Excel file using OpenXML, and I tried to transmit the file using Reponse.TransmitFile, but it is not working. When I debug the code it is not throwing any exception.
string filename = "Book1.xlsx";
try {
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
Response.ClearContent();
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Length", Server.MapPath("~/" + filename).Length.ToString());
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
Response.TransmitFile(Server.MapPath("~/"+filename));
Response.Flush();
Response.End();
}
catch (Exception e) {
Console.WriteLine(e);
}

ASP.net download file Using HttpContext.Current.Response.TransmitFile in ASP.NET

public void Downloadfile(string sFileName, string sFilePath)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String Header = "Attachment; Filename=" + sFileName;
HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache");
System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
HttpContext.Current.Response.TransmitFile(Dfile.FullName);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
I have a download button, the click will return the call and download the corresponding file download, but sometimes file returns is detailt.aspx file. I do not understand what is happening.
I need help. Thanks a lot
This has worked for me with out issue for a while now.
public void Downloadfile(string sFileName, string sFilePath)
{
var file = new System.IO.FileInfo(sFilePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + sFileName);
Response.AddHeader("Content-Length", file.Length.ToString(CultureInfo.InvariantCulture));
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}

C# Asp Net Problem with download in Chrome 12

I have a problem in my project.
I have some code for downloading content as: .doc, .zip etc
It was working fine until chrome update for version 12, after that the browser shows an error message: "Download Interrupted".
I already tested in other browsers (Chrome 11, FF and IE8) and everything works fine.
An code sample is:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("Content-Type", "application/msword");
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" + filename + ".doc");
HttpContext.Current.Response.AppendHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.Write(strBody);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
Someone know what can be happening or how I can fix that?
Ps. Sorry my english, I'm brazilian :)
According to an answer in this forum adding Response.End() solved the issue.
In your case, it would be
HttpContext.Current.Response.Write(strBody);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
I am getting my binary data from sql server database with this code and it is working in all browser;
DataTable table = SiteFileAccess.GetDataByFileID(fileId);
byte[] b = (byte[])table.Rows[0]["FileData"];
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + table.Rows[0]["FileName"].ToString());
Response.AddHeader("Content-Length", b.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(b);
Response.Flush();
Response.End();
See the above code and fix it according to your need. Let me know If you need further information
string applicationName = Session["ApplicationName_UtilityDetail"].ToString();
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(Session["DownloadLink_UtilityDetail"].ToString());
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
int bufferSize = 1;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition:", "attachment; filename=" + applicationName + ".zip");
Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString());
Response.ContentType = "application/download";
byte[] byteBuffer = new byte[bufferSize + 1];
MemoryStream memStrm = new MemoryStream(byteBuffer, true);
Stream strm = objRequest.GetResponse().GetResponseStream();
byte[] bytes = new byte[bufferSize + 1];
while (strm.Read(byteBuffer, 0, byteBuffer.Length) > 0)
{
Response.BinaryWrite(memStrm.ToArray());
Response.Flush();
}
Response.Close();
Response.End();
memStrm.Close();
memStrm.Dispose();
strm.Dispose();
Try this:
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=yourExcelFileName.xlsx;");
Response.BinaryWrite(yourByteArray);
Response.End();

Resources