Este sera el codigo .aspx
<body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Vista Previa" /> <br /> <asp:Button ID="Button2" runat="server" Text="Nombre de Archivo" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Image ID="Image1" runat="server" /> </div> </form> </body>Y este codigo sera del lado del servidor
Code Behind :
string imgname; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string L_path = "No Images"; imgname = FileUpload1.FileName; TextBox1.Text = imgname; // break the pathe, collect file type then match. string filetype; filetype = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName); if (filetype.ToLower().Contains(".jpg") || filetype.ToLower().Contains(".jpeg") || filetype.ToLower().Contains(".bmp") || filetype.ToLower().Contains(".png")) { ClientScript.RegisterStartupScript(typeof(string), "You choosen the file in wrong formated.. !!!", ""); } if (FileUpload1.PostedFile.ContentLength > 5242880) // byte size { Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('Image Size is should be Less Than 5 MB' )", true); } if (FileUpload1.HasFile) { L_path = Server.MapPath("~/Images/").ToString(); L_path += imgname; FileUpload1.SaveAs(L_path); // Temporary saved } else { Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('No Image is Selected..)", true); } Image1.ImageUrl = "~/Images/" + imgname; Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('Image is Reflected..Plz Save it)", true); // After saving into Database delete current file from Application. }
Y Después para guardarla en una Base de Datos Sql o en una carpeta en la misma aplicación
ResponderEliminar