if (File.Exists(filePath)) { string ext = Path.GetExtension(filePath).ToLower();//.html if ((new string[] { ".jpg", ".jpeg", ".gif", ".bmp", ".png" }).Contains(ext)) { context.Response.Clear(); context.Response.ContentType = "image/jpeg"; context.Response.BinaryWrite(GetImageBytes(filePath, context)); context.Response.Flush(); context.Response.End(); } else if ((new string[] { ".mp4", ".mp3", ".rm", ".wav", ".mid", ".wmv", ".avi", ".rmvb", ".mpeg", ".mpg", ".ogg" }).Contains(ext)) { context.Response.Clear(); context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8)); context.Response.ContentType = "video/mpeg4"; context.Response.BinaryWrite(GetImageBytes(filePath, context)); context.Response.Flush(); context.Response.End(); } else if ((new string[] { ".htm", ".html", "" }).Contains(ext)) { context.Response.WriteFile(filePath); } else if ((new string[] { ".xlsx", ".xls" }).Contains(ext)) { context.Response.Clear(); context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8)); context.Response.ContentType = "application/ms-excel"; context.Response.TransmitFile(filePath); context.Response.Flush(); context.Response.End(); } else if ((new string[] { ".docx", ".doc" }).Contains(ext)) { context.Response.Clear(); context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8)); context.Response.ContentType = "application/msword"; context.Response.TransmitFile(filePath); context.Response.Flush(); context.Response.End(); } else if ((new string[] { ".txt" }).Contains(ext)) { context.Response.Clear(); context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8)); context.Response.ContentType = "text/plain"; context.Response.TransmitFile(filePath); context.Response.Flush(); context.Response.End(); } else if ((new string[] { ".pdf" }).Contains(ext)) { context.Response.Clear(); context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8)); context.Response.ContentType = "application/pdf"; context.Response.TransmitFile(filePath); context.Response.Flush(); context.Response.End(); } else if ((new string[] { ".ppt", ".pptx" }).Contains(ext)) { context.Response.Clear(); context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8)); context.Response.ContentType = "application/vnd.ms-powerpoint"; context.Response.TransmitFile(filePath); context.Response.Flush(); context.Response.End(); } else { context.Response.ContentType = "application/x-zip-compressed"; context.Response.TransmitFile(filePath); } } else { context.Response.Write("file no found"); }
C#文件流下载
猜你喜欢
转载自blog.csdn.net/lixiaoer757/article/details/80311779
今日推荐
周排行