C#表单实现文件上传

<form runat="server" id="myform">
<table>
    <tr>
		<td align="right" >单位名称:</td>
		<td align="left" >
                <asp:TextBox ID="txtName" runat="server" Width="150px"  ></asp:TextBox>
        </td>
		<td >类型:</td>
		<td align="left">
            <asp:DropDownList ID="ddlType" runat="server" Width="150px" >
                <asp:ListItem Value="true" Selected="True">文字链接</asp:ListItem>
                <asp:ListItem Value="false">图片链接</asp:ListItem>
            </asp:DropDownList>
        </td>
	</tr>
        <tr>
		<td align="right" >单位类型:</td>
		<td align="left" >
                <asp:DropDownList ID="ddlCooperateType" runat="server" Width="150px" >
                <asp:ListItem Value="0" >高校</asp:ListItem>
                <asp:ListItem Value="1" Selected="True">企业</asp:ListItem>
            </asp:DropDownList>
        </td>
		<td >顺序排序:</td>
		<td align="left">
                <asp:TextBox ID="txtSort" runat="server" Width="150px"  Text="0"></asp:TextBox>
        </td>
	</tr>
    <tr>
					
		<td >链接:</td>
		<td align="left">
                <asp:TextBox ID="txtLink" runat="server" Height="17px" Width="150px"  
                    Text="http://www.qk12333.com"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                            ControlToValidate="txtLink" ErrorMessage="格式不正确!" ForeColor="Red" 
                                       
                    ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"></asp:RegularExpressionValidator>
        </td>
        <td  >图片上传:</td>
		<td align="left" >
                <asp:FileUpload ID="fileUpImage" runat="server" Width="244px"  />
        </td>
	</tr>
               
        <tr>
		<td align="right">相关描述:</td>
		<td align="left" colspan="3">
            <asp:TextBox ID="txtDesc" runat="server" TextMode="MultiLine" Width="600" 
                Height="128"></asp:TextBox>
        </td>
	</tr>
    <tr>
		<td  colspan="4" align="center" >
            <asp:Button  ID="btnFriendLink" runat="server" Text="提交" 
                    οnclick="btnFriendLink_Click" />
            <asp:Button  ID="btnRestFriendLink" runat="server" Text="重置" 
                οnclick="btnRestFriendLink_Click"   />
        </td>
</tr>
</table>
</form>

    protected void btnFriendLink_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "" || txtName.Text == null)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('单位名称不能为空!');</script>");
            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(), "", "<script>alert('请选择图片!')</script>");
                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(), "", "<script>alert('添加失败!')</script>");
            return;
        }
        else
        {
            reset();
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功!')</script>");
        }
        
    }
    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);
    
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值