1st of all add name space Using System.IO
After that following is Theory of File Upload Control in Asp.Net
1)We not need Source address only need Destination Address so we map destination address which is on server
2)We make fullpath=filepath+finename
3)(Optional)If we need file extension
4) (Optional)File size checking.
5)Save file.
After that following is Theory of File Upload Control in Asp.Net
1)We not need Source address only need Destination Address so we map destination address which is on server
2)We make fullpath=filepath+finename
3)(Optional)If we need file extension
4) (Optional)File size checking.
5)Save file.
/*Upload.aspx.cs file*/ using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.IO; public partial class AboutUs : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSave_Click(object sender, EventArgs e) { /* 1)File Upload need Using System.IO namespace 2)We not need Source address only need Destination Address so we map destination address which is on server 3)We make fullpath=filepath+finename 4)(Optional)If we need file extension 5)Save file. */ string filename = FileUpload1.FileName; string filepath = Server.MapPath("Upload" + "//"); string fullfilepath = filepath+filename; string extension = Path.GetExtension(filename); lbltext.Text = filepath; int filesize = FileUpload1.PostedFile.ContentLength/1024; int i = 0; if (extension == ".jpg" || extension == ".gif") { if (filesize < 10) { FileUpload1.SaveAs(fullfilepath); i = 1; } else { lbltext.Text = "Filesize Exceed 10KB."; } } else { lbltext.Text = extension; } if (i == 1) { Image1.ImageUrl = "~/Upload/" + filename; } } } /*Upload.aspx */ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AboutUs.aspx.cs" Inherits="AboutUs" %> <!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>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" /> </div> <asp:Label ID="lbltext" runat="server"></asp:Label> <div> <asp:Image ID="Image1" runat="server" /> </div> </form> </body> </html>
No comments:
Post a Comment