<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload Image</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Create New Directory"
onclick="Button1_Click" />
<br /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label> <br /> <br />
<asp:Label ID="lblFolderName" runat="server" Text="Folder Name"></asp:Label>
<asp:TextBox ID="txtFolderName" runat="server"></asp:TextBox> <br /><br />
<asp:Label ID="lblUploadPhoto" runat="server" Text="Upload Photo" style="margin-left:80px"></asp:Label> <br />
<asp:Image ID="imgPhoto" runat="server" Width="100px" Height="80px" style="margin-left:80px"/> <br />
<asp:FileUpload ID="fileUpload" runat="server" style="margin-left:80px"/> <br /> <br />
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" style="margin-left:80px" /> <br /> <br />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlTypes;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblFolderName.Visible = false;
txtFolderName.Visible = false;
btnSave.Visible = false;
}
private void CreateDirectoryIfNotExists(string NewDirectory)
{
try
{
if (!Directory.Exists(NewDirectory))
{
Directory.CreateDirectory(NewDirectory);
Label1.Text = "Directory Created";
}
else
{
Label1.Text = "Directory Exist";
}
}
catch (IOException _err)
{
Response.Write(_err.Message);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
lblFolderName.Visible = true;
txtFolderName.Visible = true;
btnSave.Visible=true;
Label1.Visible = false;
}
protected void btnSave_Click(object sender, EventArgs e)
{
string NewDirectory = Server.MapPath("~/Albums/" + txtFolderName.Text);
CreateDirectoryIfNotExists(NewDirectory);
Label1.Visible = true;
FileUpload img = (FileUpload)fileUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
HttpPostedFile File = fileUpload.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
string conn = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string fname = fileUpload.FileName.ToString();
string FolderName = txtFolderName.Text.Replace("'", "''");
connection.Open();
string sql = "INSERT INTO [ImageTable]([FolderName],[PhotoFileName]) Values('" + FolderName + "','" + fname.ToString() + "' )";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
connection.Close();
imgPhoto.Visible = true;
StartUpLoad();
}
else
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please select a photo before creating a profile')", true);
}
}
private void StartUpLoad()
{
string imgName = fileUpload.FileName;
string subfolder = txtFolderName.Text;
string imgPath = "~/Albums/" + subfolder + imgName;
int imgSize = fileUpload.PostedFile.ContentLength;
if (fileUpload.PostedFile != null && fileUpload.PostedFile.FileName != "")
{
if (fileUpload.PostedFile.ContentLength > 10240000)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
}
else
{
fileUpload.SaveAs(Server.MapPath(imgPath));
imgPhoto.ImageUrl = "~/" + imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Profile Photo has been uploaded!')", true);
}
}
}
}