티스토리 뷰

💼 정보 ver1.0

FileUploadControl

James Wetzel 2011. 2. 23. 16:50

public Hashtable attachFileUpload(System.Web.UI.WebControls.FileUpload fileUploadControl)
        {
            Hashtable result = new Hashtable();

            string dateString = DateTime.Now.ToShortDateString();
            string savePath = WebConfigurationManager.AppSettings["attachFileHome"].ToString() + dateString + "\\";
            makeFoler(savePath);

            int fileSize = fileUploadControl.PostedFile.ContentLength;
            string fileName = HttpUtility.HtmlEncode(fileUploadControl.FileName);
            string tempFileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + Path.GetExtension(fileName);

            string DB_fileName = dateString + "\\" + fileName;
            string DB_tempFileName = dateString + "\\" + tempFileName;           

            string fileNameExtension = System.IO.Path.GetExtension(fileName);

            //4,200,000 bytes(4MB)
            if ((fileSize < 4200000) && (fileNameExtension.ToLower().Equals(".jpg") || fileNameExtension.ToLower().Equals(".gif")))
            {
                savePath += tempFileName;
                fileUploadControl.SaveAs(savePath);

                result.Add(DB_fileName, DB_tempFileName);
            }

            return result;
        }

protected void Button1_Click(object sender, EventArgs e)

    {

        if (IsPostBack)

        {

            Boolean fileOK = false;

            String path = Server.MapPath("~/UploadedImages/");

            if (FileUpload1.HasFile)

            {

                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();

                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };

                for (int i = 0; i < allowedExtensions.Length; i++)

                {

                    if (fileExtension == allowedExtensions[i])

                    {

                        fileOK = true;

                        break;

                    }

                }

            }


            if (fileOK)

            {

                try

                {

                    FileUpload1.PostedFile.SaveAs(path+ FileUpload1.FileName);

                    Label1.Text = String.Format("File uploaded!({0:#,###0}/Byte)", FileUpload1.PostedFile.ContentLength);

                }

                catch (Exception ex)

                {

                    Label1.Text = "File could not be uploaded.";

                }

            }

            else

            {

                Label1.Text = "Cannot accept files of this type.";

            }

        }

    }

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함