Esempio n. 1
0
        public static string Excel2Html(Stream stream, string ext)
        {
            string         fileExtNoDot = ext == null ? "" : ext.TrimStart('.');
            string         folder       = Path.Combine(AttachmentManager.AttachmentRootPath, "temp");
            string         fileName     = "Excel_" + Guid.NewGuid().ToString();
            DocumentFormat excelFormat  = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), fileExtNoDot, true);

            string excelFile = Path.Combine(folder, String.Format("{0}.{1}", fileName, fileExtNoDot));

            Directory.CreateDirectory(folder);

            try
            {
                using (FileStream filestream = System.IO.File.Create(excelFile))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.CopyTo(filestream);
                }

                DocumentConverterResult result = DocumentConverter.Convert(
                    new GleamTech.IO.BackSlashPath(excelFile),
                    new InputOptions(excelFormat),
                    DocumentFormat.Html
                    );

                string htmlFile = result.OutputFiles[0];

                for (int i = 1; i < result.OutputFiles.Length; i++)
                {
                    System.IO.File.Delete(result.OutputFiles[i]);
                }

                return(htmlFile);
            }
            finally
            {
                System.IO.File.Delete(excelFile);
            }
        }
Esempio n. 2
0
        public virtual void Preview(HttpContext context)
        {
            YZRequest request = new YZRequest(context);
            bool      osfile  = request.GetBool("osfile", false);
            BPMObjectNameCollection supports = BPMObjectNameCollection.FromStringList(request.GetString("supports", null), ',');

            string filePath;
            string fileName;
            long   fileSize;
            string fileExt;

            if (osfile)
            {
                string root = request.GetString("root");
                string path = request.GetString("path");
                fileName = request.GetString("name");
                string rootPath = context.Server.MapPath(YZSoft.FileSystem.OSDirectoryManager.GetRootPath(root));
                filePath = Path.Combine(rootPath, path, fileName);

                if (!File.Exists(filePath))
                {
                    throw new Exception(String.Format(Resources.YZStrings.Aspx_Upload_FileIDNotFount, fileName));
                }

                FileInfo fileInfo = new FileInfo(filePath);
                fileSize = fileInfo.Length;
                fileExt  = fileInfo.Extension;
            }
            else
            {
                string fileId = request.GetString("fileid");

                AttachmentInfo attachment;
                using (IYZDbProvider provider = YZDbProviderManager.DefaultProvider)
                {
                    using (IDbConnection cn = provider.OpenConnection())
                    {
                        attachment = AttachmentManager.GetAttachmentInfo(provider, cn, fileId);
                    }
                }

                fileName = attachment.Name;
                fileExt  = attachment.Ext;
                fileSize = attachment.Size;
                filePath = AttachmentInfo.FileIDToPath(fileId, AttachmentManager.AttachmentRootPath);

                if (!File.Exists(filePath))
                {
                    throw new Exception(String.Format(Resources.YZStrings.Aspx_Upload_FileIDNotFount, fileId));
                }
            }

            string fileExtNoDot = fileExt == null ? "" : fileExt.TrimStart('.');

            //有请求格式并且请求格式非元文件格式
            if (supports.Count != 0 && !supports.Contains(fileExtNoDot))
            {
                //发现已有转换文件
                string existFile = null;
                foreach (string format in supports)
                {
                    string outputFile = Path.Combine(Path.GetDirectoryName(filePath), String.Format("{0}.{1}", Path.GetFileNameWithoutExtension(filePath), format));
                    if (File.Exists(outputFile))
                    {
                        existFile = outputFile;
                        break;
                    }
                }

                if (!String.IsNullOrEmpty(existFile))
                {
                    filePath = existFile;
                }
                else
                {
                    //转换文件
                    string         targetExt    = supports[0];
                    DocumentFormat targetFormat = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), targetExt, true);
                    string         outputFile   = Path.Combine(Path.GetDirectoryName(filePath), String.Format("{0}.{1}", Path.GetFileNameWithoutExtension(filePath), targetExt));
                    DocumentFormat srcFormat    = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), fileExtNoDot, true);

                    if (srcFormat == DocumentFormat.Pdf && targetFormat == DocumentFormat.Html)
                    {
                        YZSoft.Web.File.FileConvert.Pdf2Html(filePath);
                        filePath = outputFile;
                    }
                    else
                    {
                        DocumentConverterResult result = DocumentConverter.Convert(
                            new GleamTech.IO.BackSlashPath(filePath),
                            new InputOptions(srcFormat),
                            new GleamTech.IO.BackSlashPath(outputFile),
                            targetFormat
                            );
                        filePath = result.OutputFiles[0];
                    }
                }

                fileExt = Path.GetExtension(filePath);
            }

            string range       = context.Request.Headers["Range"];
            string contentType = YZMimeMapping.GetMimeType(fileExt);

            context.Response.AppendHeader("Content-Type", contentType);
            context.Response.AppendHeader("Accept-Ranges", "bytes");

            if (range == null)
            {
                FileInfo fileinfo = new FileInfo(filePath);

                //全新下载
                context.Response.AppendHeader("Content-Length", fileinfo.Length.ToString());
                //context.Response.CacheControl = HttpCacheability.Public.ToString();
                //context.Response.Cache.AppendCacheExtension("max-age=" + 365 * 24 * 60 * 60);
                //context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
                //context.Response.AppendHeader("ETag", "Never_Modify");
                //context.Response.Cache.SetETag("Never_Modify");
                //context.Response.Cache.SetLastModified(DateTime.Now.AddMinutes(-1));

                context.Response.TransmitFile(filePath);
            }
            else
            {
                //断点续传以及多线程下载支持
                string[] file_range = range.Substring(6).Split(new char[1] {
                    '-'
                });
                context.Response.Status = "206 Partial Content";
                context.Response.AppendHeader("Content-Range", "bytes " + file_range[0] + "-" + file_range[1] + "/" + fileSize.ToString());
                context.Response.AppendHeader("Content-Length", (Int32.Parse(file_range[1]) - Int32.Parse(file_range[0]) + 1).ToString());
                context.Response.TransmitFile(filePath, long.Parse(file_range[0]), (long)(Int32.Parse(file_range[1]) - Int32.Parse(file_range[0]) + 1));
            }
        }