重构代码2

还有另外一段代码需要重构,原代码:

public  string file; // 显示生成完成的swf文件
         protected  void Button1_Click( object sender, EventArgs e)
        {
             string UploadFilePath = Server.MapPath( this.HiddenField1.Value); // 保存上传的PDF或者其他可以打印的文件(DOC,DOCX等) /UploadFile/系统分析师通过的经验.doc
             string NewUploadFilePath =  string.Empty; // 转换WPS文件路径
             string ext = Path.GetExtension(UploadFilePath);
             if (( " .wps,.et,.pps,.dps,.pps ").Contains(ext))
            {
                 switch (ext)
                {
                     case  " .wps ": // wps文件
                        NewUploadFilePath = Path.ChangeExtension(UploadFilePath,  " .doc ");
                        File.Move(UploadFilePath, NewUploadFilePath);
                         break;

                     case  " .et ": // wps的表格文件
                        NewUploadFilePath = Path.ChangeExtension(UploadFilePath,  " .xls ");
                        File.Move(UploadFilePath, NewUploadFilePath);
                         break;

                     case  " .pps ":
                     case  " .dps ":
                        NewUploadFilePath = Path.ChangeExtension(UploadFilePath,  " .ppt ");
                        File.Move(UploadFilePath, NewUploadFilePath);
                         break;
                }
                 try
                {
                     if (UploadFilePath !=  null)
                    {
                         string SwfFile = strType(UploadFilePath); // 得到文件类型
                         if (SwfFile !=  null)
                        {
                             string file = NewUploadFilePath.Replace(SwfFile,  " .swf ");
                             if (!File.Exists(file))
                            {
                                 bool isconvert = ConvertPdfToSwf(NewUploadFilePath, file.Replace( " UploadFile "" SwfFolder ")); // 执行转换
                                 if (isconvert ==  true)
                                {
                                    file = Path.GetFileName(file);
                                     string FlashView =  " http://199.99.99.111:8011/FlashPrinter/Interface/FlashView.aspx ";
                                    Response.Write( "  <script> window.location.href= ' " + FlashView +  " ?dFile= " + HttpUtility.UrlEncode(file) +  " '; </script>  ");
                                }
                            }
                        }
                    }
                }
                 catch (Exception)
                {
                     throw;
                }

            }
             else
            {
                 try
                {
                     if (UploadFilePath !=  null)
                    {
                         string SwfFile = strType(UploadFilePath); // 转换后的文件后缀名
                         if (SwfFile !=  null)
                        {
                             string file = UploadFilePath.Replace(SwfFile,  " .swf ");
                             if (!File.Exists(file))
                            {
                                 bool isconvert = ConvertPdfToSwf(UploadFilePath, file.Replace( " UploadFile "" SwfFolder ")); // 执行转换
                                 if (isconvert ==  true)
                                {
                                    file = Path.GetFileName(file);
                                     string FlashView =  " http://199.99.99.111:8011/FlashPrinter/Interface/FlashView.aspx ";
                                    Response.Write( "  <script> window.location.href= ' " + FlashView +  " ?dFile= " + HttpUtility.UrlEncode(file) +  " '; </script>  ");
                                }
                            }
                        }
                    }
                }
                 catch (Exception)
                {
                     throw;
                }

            }
        }

 

Insus.NET重构之后,主方法变为:

  public  string file; // 显示生成完成的swf文件
     protected  void Button1_Click( object sender, EventArgs e)
    {
         string UploadFilePath = Server.MapPath( this.HiddenField1.Value); // 保存上传的PDF或者其他可以打印的文件(DOC,DOCX等) /UploadFile/系统分析师通过的经验.doc
        
         string ext = Path.GetExtension(UploadFilePath);

         if (ExtensionType().ContainsKey(ext))
        {
             MoveFile(ext, UploadFilePath);
        }
         else
        {
            ConvertFile(UploadFilePath);
        }
    }

 

扩展类型:

ExpandedBlockStart.gif View Code
  private Dictionary< stringstring> ExtensionType()
    { 
         Dictionary< stringstring> ex_type =  new Dictionary< stringstring>();
        ex_type.Add( " .wps "" .doc ");
        ex_type.Add( " .et "" .xls ");
        ex_type.Add( " .pps "" .ppt ");
        ex_type.Add( " .dps "" .ppt ");
         return ex_type;
    }

 

MoveFile()方法:

ExpandedBlockStart.gif View Code
  private  void MoveFile( string ext,  string uploadFilePath)
    {
         string NewUploadFilePath =  string.Empty; // 转换WPS文件路径

         if (ExtensionType().ContainsKey(ext))
        {
            NewUploadFilePath = Path.ChangeExtension(uploadFilePath, ExtensionType()[ext].ToString());
            File.Move(uploadFilePath, NewUploadFilePath);
        }

        ConvertFile(NewUploadFilePath);
    }

 

ConvertFile()方法:

ExpandedBlockStart.gif View Code
  private  void ConvertFile( string uploadFilepath)
    {
         if (uploadFilepath ==  nullreturn;

         if ( string.IsNullOrEmpty(strType(uploadFilepath)))  return;

         string SwfFile = strType(uploadFilepath);
         string file = uploadFilepath.Replace(SwfFile,  " .swf ");
         if (!File.Exists(file))  return;

         try
        {
             bool isconvert = ConvertPdfToSwf(uploadFilepath, file.Replace( " UploadFile "" SwfFolder "));
             if (isconvert)
            {
                file = Path.GetFileName(file);
                 string FlashView =  " http://199.99.99.111:8011/FlashPrinter/Interface/FlashView.aspx ";
                Response.Write( "  <script> window.location.href= ' " + FlashView +  " ?dFile= " + HttpUtility.UrlEncode(file) +  " '; </script>  ");
            }
        }
         catch (Exception)
        {
             throw;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值