zoukankan      html  css  js  c++  java
  • FreeTextBox控件上传图片到指定的绝对路径的改进

    大家都用FreeTextBox控件,本人也非常喜欢用它,特别是发布图文信息的时候非常有用,上传图片的小插件是ftb.imagegallery.aspx

    里面有源代码,我们可以更改,比如添加只有登录才能访问啊,对上传图片的类型进行检测啦(该控件已有的图片类型检查室已检查文件名后缀来进行的,会存在安全隐患,所以必须自己写),呵呵,好像跑题了,今天的重点是要让图片上传到指定的绝对路径。因为本控件,原始的代码只允许上传图片的路径是在主目录以内,但要是有必须上传到网站主目录以外的情况,就没办法了

    本人就碰到了这个情况,本人做的一个网站是前后台分开的(为了安全),也就是说前台后台在IIS中是相互独立运行的网站,而且网站文件也存在不同的盘符上面,比如前台在D盘,而后台在E盘上

    那我们在后台上传的图片文件怎么样上传到前台所在的D盘的目录中呢,因为只有这样前台才能正常访问。

    我在网上找了很久,大家都认为这个控件对这个问题是无法解决的,一直没有找到解决办法,可是我还是不甘心,因为这个问题不解决,这个网站就没法做下去了,所以就硬着头皮去看源代码

    仔细一看,看出点门道来了,我主要看ftb.imagegallery.aspx里上传图片,显示图片和目录的路径获取

    结果发现了在这几个函数中都存在一个关键性的句子:string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;

    这个句子的意思很明显就是获得当前应用程序的物理路径,我立马想到,我如果能够将这个路径改成我需要上传到的绝对路径不就可以了吗?

    非常兴奋,就修改代码尝试了

    下面我们来看看我修改了哪几个函数

    第一个:图片上传函数


    public void UploadImage_OnClick(object sender, EventArgs e) {  
        Jake.Common.Common isfile = new Jake.Common.Common();  
        if (Page.IsValid) {  
            if (CurrentImagesFolder.Value != "") {  
                if (UploadFile.PostedFile.FileName.Trim() != "") {  
                    if (IsValidFileType(UploadFile.PostedFile.FileName)) {  
                        if (isfile.IsAllowedFile(UploadFile.PostedFile, "255216") || isfile.IsAllowedFile(UploadFile.PostedFile, "7173"))//从文件流判断文件类型  
                        {  
                            try 
                            {  
                                string UploadFileName = "";  
                                string UploadFileDestination = "";  
                                UploadFileName = UploadFile.PostedFile.FileName;  
                                UploadFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + UploadFileName.Substring(UploadFileName.LastIndexOf("\\") + 1);  
                                //UploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;  
                                UploadFileDestination = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改成我指定的绝对路径,此路径我放在了Web.Config中灵活配置。  
                                UploadFileDestination += CurrentImagesFolder.Value;  
                                UploadFileDestination += "\\";  
                                UploadFile.PostedFile.SaveAs(UploadFileDestination + UploadFileName);  
                                ResultsMessage.Text = UploadSuccessMessage;  
                            }  
                            catch 
                            //(Exception ex)   
                            {  
                                //ResultsMessage.Text = "Your file could not be uploaded: " + ex.Message;  
                                ResultsMessage.Text = UploadFailureMessage;  
                            }  
                        }  
                        else { ResultsMessage.Text = InvalidFileTypeMessage; }  
                    } else {  
                        ResultsMessage.Text = InvalidFileTypeMessage;  
                    }  
                } else {  
                    ResultsMessage.Text = NoFileMessage;  
                }  
            } else {  
                ResultsMessage.Text = NoFolderSpecifiedMessage;  
            }  
        } else {  
            ResultsMessage.Text = InvalidFileTypeMessage;  
              
        }  
        DisplayImages();  

    public void UploadImage_OnClick(object sender, EventArgs e) {
        Jake.Common.Common isfile = new Jake.Common.Common();
        if (Page.IsValid) {
      if (CurrentImagesFolder.Value != "") {
       if (UploadFile.PostedFile.FileName.Trim() != "") {
        if (IsValidFileType(UploadFile.PostedFile.FileName)) {
                        if (isfile.IsAllowedFile(UploadFile.PostedFile, "255216") || isfile.IsAllowedFile(UploadFile.PostedFile, "7173"))//从文件流判断文件类型
                        {
                            try
                            {
                                string UploadFileName = "";
                                string UploadFileDestination = "";
                                UploadFileName = UploadFile.PostedFile.FileName;
                                UploadFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + UploadFileName.Substring(UploadFileName.LastIndexOf("\\") + 1);
                                //UploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;
                                UploadFileDestination = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改成我指定的绝对路径,此路径我放在了Web.Config中灵活配置。
                                UploadFileDestination += CurrentImagesFolder.Value;
                                UploadFileDestination += "\\";
                                UploadFile.PostedFile.SaveAs(UploadFileDestination + UploadFileName);
                                ResultsMessage.Text = UploadSuccessMessage;
                            }
                            catch
                            //(Exception ex)
                            {
                                //ResultsMessage.Text = "Your file could not be uploaded: " + ex.Message;
                                ResultsMessage.Text = UploadFailureMessage;
                            }
                        }
                        else { ResultsMessage.Text = InvalidFileTypeMessage; }
        } else {
         ResultsMessage.Text = InvalidFileTypeMessage;
        }
       } else {
        ResultsMessage.Text = NoFileMessage;
       }
      } else {
       ResultsMessage.Text = NoFolderSpecifiedMessage;
      }
     } else {
      ResultsMessage.Text = InvalidFileTypeMessage;
      
     }
     DisplayImages();
    }

    第二个函数(图片删除函数)


    public void DeleteImage_OnClick(object sender, EventArgs e) {  
        if (FileToDelete.Value != "" && FileToDelete.Value != "undefined") {  
            try {  
                //string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;  
                string AppPath = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改成指定绝对路径  
                System.IO.File.Delete(AppPath  + CurrentImagesFolder.Value + "\\" + FileToDelete.Value);  
                ResultsMessage.Text = "已删除: " + FileToDelete.Value;  
            } catch//(Exception ex)   
            {             
                ResultsMessage.Text = "删除失败!";  
            }  
        } else {  
            ResultsMessage.Text = NoFileToDeleteMessage;  
        }  
        DisplayImages();  

    public void DeleteImage_OnClick(object sender, EventArgs e) {
     if (FileToDelete.Value != "" && FileToDelete.Value != "undefined") {
      try {
       //string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
                string AppPath = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改成指定绝对路径
       System.IO.File.Delete(AppPath  + CurrentImagesFolder.Value + "\\" + FileToDelete.Value);
       ResultsMessage.Text = "已删除: " + FileToDelete.Value;
      } catch//(Exception ex)
            {   
       ResultsMessage.Text = "删除失败!";
      }
     } else {
      ResultsMessage.Text = NoFileToDeleteMessage;
     }
     DisplayImages();
    }

    第三个函数:((返回文件列表)


    private string[] ReturnFilesArray() {  
        if (CurrentImagesFolder.Value != "") {  
            try {  
                //string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;  
                string AppPath = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改成指定的绝对路径  
                string ImageFolderPath = AppPath + CurrentImagesFolder.Value;  
                string[] FilesArray = System.IO.Directory.GetFiles(ImageFolderPath,"*");  
                return FilesArray;  
                  
                  
            } catch {  
              
                return null;  
            }  
        } else {  
            return null;  
        }  
     

    private string[] ReturnFilesArray() {
     if (CurrentImagesFolder.Value != "") {
      try {
       //string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
                string AppPath = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改成指定的绝对路径
       string ImageFolderPath = AppPath + CurrentImagesFolder.Value;
       string[] FilesArray = System.IO.Directory.GetFiles(ImageFolderPath,"*");
       return FilesArray;
       
       
      } catch {
      
       return null;
      }
     } else {
      return null;
     }

    }

    第四个函数:(返回文件夹列表)


    private string[] ReturnDirectoriesArray() {  
        if (CurrentImagesFolder.Value != "") {  
            try {  
                //string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;  
                string AppPath = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改处  
                string CurrentFolderPath = AppPath + CurrentImagesFolder.Value;  
                string[] DirectoriesArray = System.IO.Directory.GetDirectories(CurrentFolderPath,"*");  
                return DirectoriesArray ;  
            } catch {  
                return null;  
            }  
        } else {  
            return null;  
        }  

    private string[] ReturnDirectoriesArray() {
     if (CurrentImagesFolder.Value != "") {
      try {
       //string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
                string AppPath = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改处
       string CurrentFolderPath = AppPath + CurrentImagesFolder.Value;
       string[] DirectoriesArray = System.IO.Directory.GetDirectories(CurrentFolderPath,"*");
       return DirectoriesArray ;
      } catch {
       return null;
      }
     } else {
      return null;
     }
    }

    第五个函数:(显示图片函数)


    public void DisplayImages() {  
        string[] FilesArray = ReturnFilesArray();  
        string[] DirectoriesArray = ReturnDirectoriesArray();  
        //string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;  
        string AppPath = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改处  
        string AppUrl;  
          
        //Get the application's URL  
        if (Request.ApplicationPath == "/")  
            AppUrl = Request.ApplicationPath;  
        else 
            AppUrl = Request.ApplicationPath + "/";  
          
        GalleryPanel.Controls.Clear();  
        if ( (FilesArray == null || FilesArray.Length == 0) && (DirectoriesArray == null || DirectoriesArray.Length == 0) ) {  
            gallerymessage.Text = NoImagesMessage + ": " + RootImagesFolder.Value;  
        } else {  
            string ImageFileName = "";  
            string ImageFileLocation = "";  
     
    //。。。。。。。下面代码略 
    public void DisplayImages() {
     string[] FilesArray = ReturnFilesArray();
     string[] DirectoriesArray = ReturnDirectoriesArray();
     //string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
        string AppPath = LTP.Common.ConfigHelper.GetConfigString("UpNewsImagePyPath");//修改处
     string AppUrl;
     
     //Get the application's URL
     if (Request.ApplicationPath == "/")
      AppUrl = Request.ApplicationPath;
     else
      AppUrl = Request.ApplicationPath + "/";
     
     GalleryPanel.Controls.Clear();
     if ( (FilesArray == null || FilesArray.Length == 0) && (DirectoriesArray == null || DirectoriesArray.Length == 0) ) {
      gallerymessage.Text = NoImagesMessage + ": " + RootImagesFolder.Value;
     } else {
      string ImageFileName = "";
      string ImageFileLocation = "";

    //。。。。。。。下面代码略
     

    好,到此为止呢,我们已经可以将图片上传到我们指定的绝对路径了,这个绝对路径就是我前台网站里的图片存储目录

    可是这样还有个小问题,就是我们插入图片时时双击图片,然后图片就到了文本编辑器里了,看HTML代码就可以看到,返回的路径是,当前网站的绝对访问路径,也就是:http://192.168.1.5:8081/upimages/111.gif类似的路径

    但是如果我们不修改此返回路径,那插入到编辑器里的图片将是无法显示的,因为当前网站下面根本不存在“upimages/111.gif”这个文件夹和文件,所以我们的修改成前台网站的访问地址,比如我的前台访问地址就是http://192.168.1.5:8082/upimages/111.gif

    那我们怎么修改返回地址呢,我们仔细看代码,我们发现有


    myImageHolder.Attributes["ondblclick"]="returnImage('ImageFileLocation.Replace("\\","/") + "','" + myImage.Width.ToString() + "','" + myImage.Height.ToString() + "');";    
                    myImageHolder.Controls.Add(myHtmlImage); 
    myImageHolder.Attributes["ondblclick"]="returnImage('ImageFileLocation.Replace("\\","/") + "','" + myImage.Width.ToString() + "','" + myImage.Height.ToString() + "');"; 
        myImageHolder.Controls.Add(myHtmlImage);

    这个里面的事件是鼠标双击事件,很明显returnImage里就是要返回的图片访问路径啊,所以我们更改成


    string address = LTP.Common.ConfigHelper.GetConfigString("Address");//在web.config文件里配置前台访问IP或地址  
    myImageHolder.Attributes["ondblclick"]="returnImage('"+address + ImageFileLocation.Replace("\\","/") + "','" + myImage.Width.ToString() + "','" + myImage.Height.ToString() + "');";    
                    myImageHolder.Controls.Add(myHtmlImage); 
    string address = LTP.Common.ConfigHelper.GetConfigString("Address");//在web.config文件里配置前台访问IP或地址
    myImageHolder.Attributes["ondblclick"]="returnImage('"+address + ImageFileLocation.Replace("\\","/") + "','" + myImage.Width.ToString() + "','" + myImage.Height.ToString() + "');"; 
        myImageHolder.Controls.Add(myHtmlImage);

    这样双击返回的就是我们想要的http://192.168.1.5:8082/upimages/111.gif啦,在后台编辑时显示正常,同样前台访问肯定也很正常了,呵呵,至此修改结束

    问题解决,因为在网上没找到相关资料

    所以在这里记录下来

    希望对有需求的朋友起到抛砖引玉的左右

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/j_jake/archive/2009/09/01/4508776.aspx

  • 相关阅读:
    平时十二测
    无题十四
    暑假第十测
    无题十三
    noip错题集
    无题十二
    BZOJ整理
    志愿者招募
    修车
    任务安排
  • 原文地址:https://www.cnblogs.com/jake/p/1592524.html
Copyright © 2011-2022 走看看