C#フォーム実装ファイルアップロード

4678 ワード

:
:
:
    protected void btnFriendLink_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "" || txtName.Text == null)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('        !');");
            return;
        }
        string message = string.Empty;
        string imageSrc = string.Empty;
        //    ,    
        if (ddlType.SelectedValue == "false"||(fileUpImage.FileName!=null&&fileUpImage.FileName!=string.Empty))
        {
            string imageName = DateTime.Now.ToFileTime().ToString();
            if (imageName == "" || imageName == null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('     !')");
                return;
            }
            string contentType = imageName.Substring(imageName.LastIndexOf(".") + 1);
            string folderPath = Server.MapPath("../uploadFile/FriendLink/");
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            } 
            string path = Server.MapPath("../uploadFile/FriendLink/") + imageName;
            imageSrc = "uploadFile/FriendLink/" + imageName + "." + fileUpImage.FileName.Substring(fileUpImage.FileName.LastIndexOf(".") + 1); ;
            UploadFile.Upload(fileUpImage, path, ref message);
        }
       
        FriendsLink upImage = InsertToFriendsLink(imageSrc);
        if (upImage == null)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('    !')");
            return;
        }
        else
        {
            reset();
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('    !')");
        }
        
    }
    private FriendsLink InsertToFriendsLink(string imageSrc) {
        FriendsLink friendsLink = new FriendsLink();
        friendsLink.Name = StringSub.SubTitle(txtName.Text);
        friendsLink.Link = txtLink.Text;
        friendsLink.Desc = StringSub.SubDescription(txtDesc.Text.Trim());
        friendsLink.ImageSrc = imageSrc;
        friendsLink.SortId = Convert.ToInt32(txtSort.Text);
        friendsLink.CooperateType = Convert.ToInt32(ddlCooperateType.SelectedValue);
        friendsLink.LinkType = Convert.ToBoolean(ddlType.SelectedValue);
        return FriendsLinkManager.AddFriendsLink(friendsLink);
    
    }