asp.net,C#,FileUpload控件文件上传简单实例,vs2010
2013-02-17 11:32
956 查看
文件上传是最常用的B/S项目功能,在FileUpload控件出来之前只能使用html控件的File控件,这样在form中就需要加入【 enctype="multipart/form-data"】。FileUpload出来后就不需要了,form就不需要做上面的更改了。
实例如下,
up1.aspx代码
up1.aspx.cs代码
实例如下,
up1.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="up1.aspx.cs" Inherits="up1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function checkform() { var strs = document.getElementById("FileUpload1").value; if (strs == "") { alert("请选择要上传的图片!"); return false; } var n1 = strs.lastIndexOf('.') + 1; var fileExt = strs.substring(n1, n1 + 3).toLowerCase() if (fileExt != "jpg" && fileExt != "bmp" && fileExt != "png") { alert('目前系统仅支持jpg、bmp、png后缀图片上传!'); return false; } } </script> </head> <body> <form id="form1" runat="server"> <asp:FileUpload ID="FileUpload1" runat="server" Width="220px" /> <asp:Button ID="Button1" runat="server" CssClass="button" OnClick="Button1_Click" Text="上传" /> </form> </body> </html>
up1.aspx.cs代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; public partial class up1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Attributes["onclick"] = "return checkform();"; } protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string upPath = "/up/"; //上传文件路径 int upLength = 5; //上传文件大小 string upFileType = "|image/bmp|image/x-png|image/pjpeg|image/gif|image/png|image/jpeg|"; string fileContentType = FileUpload1.PostedFile.ContentType; //文件类型 if (upFileType.IndexOf(fileContentType.ToLower()) > 0) { string name = FileUpload1.PostedFile.FileName; // 客户端文件路径 FileInfo file = new FileInfo(name); string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + file.Extension; // 文件名称,当前时间(yyyyMMddhhmmssfff) string webFilePath = Server.MapPath(upPath) + fileName; // 服务器端文件路径 string FilePath = upPath + fileName; //页面中使用的路径 if (!File.Exists(webFilePath)) { if ((FileUpload1.FileBytes.Length / (1024 * 1024)) > upLength) { ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('大小超出 " + upLength + " M的限制,请处理后再上传!');", true); return; } try { FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件 ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件上传成功');", true); } catch (Exception ex) { ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件上传失败" + ex.Message + "');", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件已经存在,请重命名后上传');", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件类型不符" + fileContentType + "');", true); } } } }
相关文章推荐
- asp.net,C#,html控件的File控件文件上传简单实例,vs2010
- asp.net,C#,html控件的File控件实现多文件上传简单实例,vs2010
- ASP.NET中FileUpload文件上传控件应用实例
- ASP.NET中FileUpload文件上传控件应用实例
- Asp.net的FileUpload控件的文件上传与Extjs文件上传的简单Demo
- ASP.NET 2.0使用FileUpload控件上传文件示例
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- (转)ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器
- ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器
- FileUpload控件 上传文件大小 asp.net
- 用ASP.net(c#)上传各种文件的实例
- asp.net fileupload控件上传文件与多文件上传
- asp.net图片上传-简单实用(VS2008 FileUpload控件+ASP.NET2.0)按时间+三位随机数重命名
- Asp.Net的FileUpload类实现上传文件实例
- Asp.net_文件上传控件FileUpload用法初步
- asp.net html控件的File控件实现多文件上传实例分享
- 用ASP.NET WebForm的FileUpload控件上传文件
- ASP.NET 2.0使用FileUpload控件上传文件示例
- asp.net图片上传-简单实用(VS2008 FileUpload控件+ASP.NET2.0)按时间+三位随机数重命名
- Asp.Net的FileUpload类实现上传文件实例