How to save client side image file to server side
This code shold be copied in .aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>File Upload Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="10" cellspacing="0" border="2" width="40%" height="100px">
<tr>
<td align="center" valign="top" style="padding-top:20px; background-color: #ffccff;">
<asp:FileUpload ID="FileUpload1" runat="server" BackColor="#C0FFFF" BorderColor="Cyan" ForeColor="Green" /><br /><br />
<asp:Button ID="Button1" runat="server" Onclick="Button_click" Text="Upload File" BackColor="#C0C0FF" BorderColor="#8080FF" BorderStyle="Outset" />
<asp:RegularExpressionValidator id="FileUpLoadValidator" runat="server"
ErrorMessage="Upload Jpegs and Gifs only."
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$"
ControlToValidate="FileUpload1">
</asp:RegularExpressionValidator>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
The below code should be copied in the code behind file:
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;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void Button_click(object sender, EventArgs e)
{
string str1 = FileUpload1.PostedFile.FileName;
string str2 = Path.GetFileName(str1);
string str3 = @"d:/puru/photo"; //give the path where you want to upload the file.
FileUpload1.PostedFile.SaveAs(Path.Combine(str3, str2));
}
}