Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
I written the below code for downloading the attachments. when i ran the code i can successfully download the files in Mozilla firefox, IE but this is not working in Google Chrome.


C#
string base64FileString = result;
                        byte[] binaryFile = Convert.FromBase64String(base64FileString);
                        Response.ContentType = "application/octet-stream";
                        Response.AddHeader("content-disposition", String.Format("attachment; filename=\"{0}\"", Request.QueryString["FILENAME"]));
                        Response.Clear();
                        Response.BufferOutput = false;
                        Response.ClearContent();
                        Response.BinaryWrite(binaryFile);
                        Response.Flush();
                        Response.Close();

C#
an anyone please help me what changes need to do for downloading in Chrome ?


What I have tried:

I tried to include the content length to the response header but still getting the same error.
Posted
Updated 15-Oct-16 21:10pm

1 solution

I am not sure what the problem is but this works for me in both you could try it out ....Let me know if it works out
C#
if (File.Exists(filepath)) {
	
	string filename = Path.GetFileName(filepath);
	Response.Clear();
	Response.ContentType = GetMimeTypeByFileName(filename);
	Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
	Response.AddHeader("Content-Length", mfile.Length.ToString());
	Response.TransmitFile(filepath);
	Response.End();
}



The Function GetMimeTypeByFileName(filename) is below

C#
public string GetMimeTypeByFileName(string sFileName)
{
	string sMime = "application/octet-stream";

	string sExtension = System.IO.Path.GetExtension(sFileName);
	if (!string.IsNullOrEmpty(sExtension)) {
		sExtension = sExtension.Replace(".", "");
		sExtension = sExtension.ToLower();

		if (sExtension == "xls" || sExtension == "xlsx") {
			sMime = "application/ms-excel";
		} else if (sExtension == "pdf") {
			sMime = "application/pdf";
		} else if (sExtension == "doc" || sExtension == "docx") {
			sMime = "application/msword";
		} else if (sExtension == "ppt" || sExtension == "pptx") {
			sMime = "application/ms-powerpoint";
		} else if (sExtension == "rtf") {
			sMime = "application/rtf";
		} else if (sExtension == "zip") {
			sMime = "application/zip";
		} else if (sExtension == "mp3") {
			sMime = "audio/mpeg";
		} else if (sExtension == "bmp") {
			sMime = "image/bmp";
		} else if (sExtension == "gif") {
			sMime = "image/gif";
		} else if (sExtension == "jpg" || sExtension == "jpeg") {
			sMime = "image/jpeg";
		} else if (sExtension == "png") {
			sMime = "image/png";
		} else if (sExtension == "tiff" || sExtension == "tif") {
			sMime = "image/tiff";
		} else if (sExtension == "txt") {
			sMime = "text/plain";
		}
	}

	return sMime;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900