asp.net文件上传解决方案(图片上传、单文件上传

小编之前也介绍了许多ASP.NET文件上传的解决案例,今天来个asp.net文件上传大集合。

1 使用标准HTML来进行图片上传
前台代码:

<body> <form runat="server"> <div> <table> <tr> <td colspan="2" > 使用标准HTML来进行图片上传</td> </tr> <tr> <td> <input type="file" runat="server" /></td> <td> <asp:Button runat="server" Text="上传图片" /></td> </tr> <tr> <td colspan="2" > <asp:Label runat="server" ForeColor="Red"></asp:Label></td> </tr> </table> </div> </form> </body>


后台代码:

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void UploadButton_Click(object sender, EventArgs e) { string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名 //string uploadName = InputFile.PostedFile.FileName; string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复 if (InputFile.Value != "") { int idx = uploadName.LastIndexOf("."); string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名 pictureName = DateTime.Now.Ticks.ToString() + suffix; } try { if (uploadName != "") { string path = Server.MapPath("~/images/"); InputFile.PostedFile.SaveAs(path + pictureName); } } catch (Exception ex) { Response.Write(ex); } } }

2 单文件上传
这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件, 很不好用。其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本。
前台代码:

<body> <form runat="server"> <div> <table> <tr> <td colspan=2> <strong><span>最简单的单文件上传</span></strong></td> </tr> <tr> <td> <asp:FileUpload runat="server" /></td> <td align=left> <asp:Button runat="server" Text="上传图片" /></td> </tr> <tr> <td colspan=2> <asp:Label runat="server" ForeColor="Red"></asp:Label></td> </tr> </table> </div> </form> </body>

后台代码:

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void FileUpload_Button_Click(object sender, EventArgs e) { try { if (FileUpload1.PostedFile.FileName == "") //if (FileUpload1.FileName == "") //if (!FileUpload1.HasFile) //获取一个值,该值指示 System.Web.UI.WebControls.FileUpload 控件是否包含文件。包含文件,则为 true;否则为 false。 { this.Upload_info.Text = "请选择上传文件!"; } else { string filepath = FileUpload1.PostedFile.FileName; //得到的是文件的完整路径,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg //string filepath = FileUpload1.FileName; //得到上传的文件名20022775_m.jpg string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg string serverpath = Server.MapPath("~/images/") + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为 this.Upload_info.Text = "上传成功!"; } } catch (Exception ex) { this.Upload_info.Text = "上传发生错误!原因是:" + ex.ToString(); } } }


3、多文件上传
前台代码:

<body> <form runat="server"> <div> <table> <tr> <td> 多文件上传</td> <td> </td> </tr> <tr> <td> <asp:FileUpload runat="server" /> </td> <td> </td> </tr> <tr> <td> <asp:FileUpload runat="server" /></td> <td> </td> </tr> <tr> <td> <asp:FileUpload runat="server" /></td> <td> </td> </tr> <tr> <td> <asp:Button runat="server" Text="一起上传" /> <asp:Label runat="server" ForeColor="Red"></asp:Label></td> <td> </td> </tr> </table> </div> </form> </body>


后台代码:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwpwy.html