C#表单实现文件上传

单位名称: 类型: 文字链接 图片链接
单位类型: 高校 企业 顺序排序:
链接: 图片上传:
相关描述:

    protected void btnFriendLink_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "" || txtName.Text == null)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "");
            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(), "", "");
                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(), "", "");
            return;
        }
        else
        {
            reset();
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "");
        }
        
    }
    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);
    
    }


你可能感兴趣的:(asp.net,C#基础)