C#表单实现文件上传
来源:互联网 发布:孙子兵法 知乎 编辑:程序博客网 时间:2024/04/28 13:26
<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="提交" onclick="btnFriendLink_Click" /> <asp:Button ID="btnRestFriendLink" runat="server" Text="重置" onclick="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); }