C#アップロード画像保存する画像名を返す

6786 ワード

    /// 
    ///     
    /// 
    ///       
    ///    
    public static void Delfile(string saveFilePath, string imgPath) 
    {
        //      
        string pathname = System.Web.HttpContext.Current.Server.MapPath(saveFilePath) + "/" + imgPath;
        string fname = pathname.Substring(pathname.LastIndexOf("\\") + 1);
        if (File.Exists(pathname))
        {
            try
            {
                File.Delete(pathname);
            }
            catch (Exception error)
            {
                buss.ShowMsgBox("        !   :" + error.Message);               
            }
        }
    }





    /// 
    ///     ,         
    /// 
    ///       
    ///           
    ///            
    ///         
    public static string Upimgfile(FileUpload fulPhoto, string saveFilePath, string imgPath)
    {
        if (fulPhoto.HasFile)
        {
            try
            {
                //     
                if (imgPath != "")
                {
                    //      
                    string pathname = System.Web.HttpContext.Current.Server.MapPath(saveFilePath) + "/" + imgPath;
                    string fname = pathname.Substring(pathname.LastIndexOf("\\") + 1);
                    if (File.Exists(pathname))
                    {
                        try
                        {
                            File.Delete(pathname);
                        }
                        catch (Exception error)
                        {
                            buss.ShowMsgBox("        !   :" + error.Message);
                            return "";
                        }
                    }
                }


                //     
                string fileName = fulPhoto.FileName;
                string savePath = System.Web.HttpContext.Current.Server.MapPath(saveFilePath);
                string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);

                //    
                string imgName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + fileName;
                //      
                string fullpath = savePath + "\\" + imgName;
                //    
                fulPhoto.SaveAs(fullpath);
                imgPath = imgName;
            }
            catch (Exception ex)
            {
                buss.ShowMsgBox("      !  :" + ex.Message);
                return "";
            }
        }

        return imgPath;
    }


    /// 
    ///     ,         
    /// 
    ///       
    ///           
    ///            
    ///         
    public static string Upimgfile(FileUpload fulPhoto, string saveFilePath, string imgPath,string aa)
    {
        if (fulPhoto.HasFile)
        {
            try
            {
                //     
                if (imgPath != "")
                {
                    //      
                    string pathname = System.Web.HttpContext.Current.Server.MapPath(saveFilePath) + "/" + imgPath;
                    string fname = pathname.Substring(pathname.LastIndexOf("\\") + 1);
                    if (File.Exists(pathname))
                    {
                        try
                        {
                            File.Delete(pathname);
                        }
                        catch (Exception error)
                        {
                            buss.ShowMsgBox("        !   :" + error.Message);
                            return "";
                        }
                    }
                }


                //     
                string fileName = fulPhoto.FileName;
                string savePath = System.Web.HttpContext.Current.Server.MapPath(saveFilePath);
                string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);

                //    
                string imgName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + aa + fileName;
                //      
                string fullpath = savePath + "\\" + imgName;
                //    
                fulPhoto.SaveAs(fullpath);
                imgPath = imgName;
            }
            catch (Exception ex)
            {
                buss.ShowMsgBox("      !  :" + ex.Message);
                return "";
            }
        }

        return imgPath;
    }


 /// 
    ///     ,         
    /// 
    ///       
    ///           
    ///            
    ///         
    public static string Upimgfile2(FileUpload fulPhoto, string saveFilePath, string imgPath,string numb)
    {
       if (fulPhoto.HasFile)
        {
            try
            {
                //     
                if (imgPath != "")
                {
                    //      
                    string pathname = System.Web.HttpContext.Current.Server.MapPath(saveFilePath) + "/" + imgPath;
                    string fname = pathname.Substring(pathname.LastIndexOf("\\") + 1);
                    if (File.Exists(pathname))
                    {
                        try
                        {
                            File.Delete(pathname);
                        }
                        catch (Exception error)
                        {
                            buss.ShowMsgBox("        !   :" + error.Message);
                            return "";
                        }
                    }
                }


                //     
                string fileName = fulPhoto.FileName;
                string savePath = System.Web.HttpContext.Current.Server.MapPath(saveFilePath);
                string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);
                
                //    
                string imgName = System.DateTime.Now.ToString("yyyyMMddhhmmss")+ numb + fileName;
                //      
                string fullpath = savePath + "\\" + imgName;
                //    
                fulPhoto.SaveAs(fullpath);
                imgPath = imgName;
            }
            catch (Exception ex)
            {
                buss.ShowMsgBox("      !  :" + ex.Message);
                return "";
            }
        }

        return imgPath;
    }