欢迎来到银龙的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

GetContentType

文件下载

文件下载

                var fileDownloadName = file.FlieName + System.IO.Path.GetExtension(filePath);
                var mimeType = GetContentType(System.IO.Path.GetExtension(filePath)); ;
                return File(FileStream(filePath), mimeType, fileDownloadName);

 

        public Stream FileStream(string path)
        {
            using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
            {
                WebClient webClient = new WebClient();

                var obj = webClient.OpenRead(path);
                return obj;
            }
        }
        private string GetContentType(string fileExt)
        {
            switch (fileExt)
            {
                case ".doc":
                    return "application/msword";
                case "docx":
                    return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                case ".rtf":
                    return "application/rtf";
                case ".xls":
                    return "application/vnd.ms-excel";
                case ".xlsx":
                    return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                case ".ppt":
                    return "application/vnd.ms-powerpoint";
                case ".pptx":
                    return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                case ".pdf":
                    return "application/pdf";
                case ".swf":
                    return "application/x-shockwave-flash";
                case ".rar":
                    return "application/octet-stream";
                case ".zip":
                    return "application/x-zip-compressed";
                case ".mp3":
                    return "audio/mpeg";
                case ".gif":
                    return "image/gif";
                case ".png":
                    return "image/png";
                case ".jpeg":
                    return "image/jpeg";
                case ".jpg":
                    return "image/jpeg";
                case ".jpe":
                    return "image/jpeg";
                case ".txt":
                    return "text/plain";
                case ".bmp":
                    return "image/jpeg";
                case ".exe":
                    return "application/octet-stream";

                default:
                    return "application/octet-stream";

            }

        }

 

posted on 2019-04-17 15:23  银龙科技  阅读(838)  评论(0编辑  收藏  举报

导航