从Google Chrome下载文件时出现网络错误 [英] Failed network error while downloading the files from Google chrome

查看:2051
本文介绍了从Google Chrome下载文件时出现网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.







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();




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.

推荐答案

我不知道问题是什么,但这对我有用,你可以尝试一下....让我知道它是否成功

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
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();
}







函数GetMimeTypeByFileName(文件名)低于






The Function GetMimeTypeByFileName(filename) is below

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;
}


这篇关于从Google Chrome下载文件时出现网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆