Asp.Net画像のアップロード方法

3666 ワード

private void GetPic(TextBox tbox, FileUpload fuload, int kid)
    {
        if (fuload.HasFile && !string.IsNullOrEmpty(tbox.Text))
        {
            //   (         )
            string extend = Path.GetExtension(fuload.FileName).ToLower();
            //        
            string fileType = fuload.PostedFile.ContentType;
            if (PiCont.IsPictureExtend(fileType))
            {
                //        
                int length = fuload.PostedFile.ContentLength;
                if (length > 5120000)
                {
                    Label1.Text = abc.ErrorImageBig;
                }
                else
                {
                    //         ( , )
                    int w = 280;
                    int h = 220;
                    //      
                    string datefolder = DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/";
                    //      (   )
                    string datename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + extend;
                    //      (        )
                    string imagename = string.Format("{0}{1}", datefolder, datename);
                    //      (   )
                    string dir = Server.MapPath("~/theImage/img/" + datefolder);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    string bigPath = dir + datename;
                    //      (   )
                    string sdir = Server.MapPath("~/theImage/simg/" + datefolder);
                    if (!Directory.Exists(sdir))
                    {
                        Directory.CreateDirectory(sdir);
                    }
                    string smallPath = sdir + datename;
                    //        (     )
                    string emptyDIR = Server.MapPath("~/empty/");
                    if (!Directory.Exists(emptyDIR))
                    {
                        Directory.CreateDirectory(emptyDIR);
                    }
                    string emptyPath = emptyDIR + datename;
                    //            (     )
                    string emptyWaterDIR = Server.MapPath("~/empty/water/");
                    if (!Directory.Exists(emptyWaterDIR))
                    {
                        Directory.CreateDirectory(emptyWaterDIR);
                    }
                    string emptyWaterPath = emptyWaterDIR + datename;
                    //       
                    string waterPath = Server.MapPath("~/ystyle/images/water.png");
                    //      
                    fuload.SaveAs(emptyPath);
                    //      
                    MakeThumbnail mt = new MakeThumbnail();
                    mt.AddWaterPic(emptyPath, waterPath, emptyWaterPath);

                    //    
                    string size = string.Empty;
                    mt.makeThumbnail(emptyWaterPath, bigPath, 700, 525, "HW", length / 1024, out size);
                    mt.makeThumbnail(emptyPath, smallPath, w, h, "HW");
                }
            }
        }
    }