Exemplo n.º 1
0
        /// <summary>
        /// 文件预览
        /// </summary>
        /// <param name="fileId">文件ID</param>
        /// <returns></returns>
        public void PreviewFile(string fileId)
        {
            var    data     = fileInfoBLL.GetEntity(fileId);
            string filename = Server.UrlDecode(data.F_FileName);              //客户端保存的文件名
            string filepath = DirFileHelper.GetAbsolutePath(data.F_FilePath); //路径

            if (data.F_FileType == "xlsx" || data.F_FileType == "xls")
            {
                filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
                if (!DirFileHelper.IsExistFile(filepath))
                {
                    filePreviewIBLL.GetExcelData(DirFileHelper.GetAbsolutePath(data.F_FilePath));
                }
            }
            if (data.F_FileType == "docx" || data.F_FileType == "doc")
            {
                filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
                if (!DirFileHelper.IsExistFile(filepath))
                {
                    filePreviewIBLL.GetWordData(DirFileHelper.GetAbsolutePath(data.F_FilePath));
                }
            }
            FileStream files = new FileStream(filepath, FileMode.Open);

            byte[] fileByte = new byte[files.Length];
            files.Read(fileByte, 0, fileByte.Length);
            files.Close();
            System.IO.MemoryStream ms = new MemoryStream(fileByte, 0, fileByte.Length);
            Response.ClearContent();
            switch (data.F_FileType)
            {
            case "jpg":
                Response.ContentType = "image/jpeg";
                break;

            case "gif":
                Response.ContentType = "image/gif";
                break;

            case "png":
                Response.ContentType = "image/png";
                break;

            case "bmp":
                Response.ContentType = "application/x-bmp";
                break;

            case "jpeg":
                Response.ContentType = "image/jpeg";
                break;

            case "doc":
                Response.ContentType = "application/pdf";
                break;

            case "docx":
                Response.ContentType = "application/pdf";
                break;

            case "ppt":
                Response.ContentType = "application/x-ppt";
                break;

            case "pptx":
                Response.ContentType = "application/x-ppt";
                break;

            case "xls":
                Response.ContentType = "application/pdf";
                break;

            case "xlsx":
                Response.ContentType = "application/pdf";
                break;

            case "pdf":
                Response.ContentType = "application/pdf";
                break;

            case "txt":
                Response.ContentType = "text/plain";
                break;

            case "csv":
                Response.ContentType = "";
                break;

            default:
                Response.ContentType = "application/pdf";
                break;
            }
            Response.Charset = "GB2312";
            Response.BinaryWrite(ms.ToArray());
        }