html
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<script type="text/javascript">
function showFileSelector() {
var fileControl = document.getElementById("fileLogo");
fileControl.click();
return false;
}
function getFileAndPath() {
var fileControl = document.getElementById("fileLogo");
var lblUploadedLogo = document.getElementById("lblUploadedLogo");
lblUploadedLogo.innerHTML = fileControl.value;
$("#manageFile").show();
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table width="100%" cellpadding="10px" cellspacing="0">
<tr>
<td valign="top">
<div style="width: 76px; height: 78px; background-color: #F2F2F2; border: 1px solid #CFCFCF;
padding: 10px;">
<asp:Image ID="imgLogo" ImageUrl="~/images/Default_Image.jpg" ToolTip="Click to upload / change Image."
runat="server" Width="76px" Height="78px" onclick="return showFileSelector()"
Style="cursor: pointer" />
<asp:FileUpload ClientIDMode="Static" ID="fileLogo" runat="server" onchange="getFileAndPath();"
Style="display: none;" />
<asp:HiddenField ID="hdnLogoPath" runat="server" />
<asp:HiddenField ID="hdnUploadedLogo" runat="server" />
</div>
<div id="manageFile" style="display: none; float: left">
<asp:Label ID="lblUploadedLogo" ClientIDMode="Static" runat="server" Text="Label"></asp:Label>
<asp:LinkButton ID="lnkUploadLogo" runat="server" OnClick="lnkUploadLogo_Click">Upload</asp:LinkButton>
</div>
</td>
</tr>
</table>
</asp:Content>
//////////
c#:
protected void lnkUploadLogo_Click(object sender, EventArgs e)
{
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + Path.GetFileName(fileLogo.PostedFile.FileName);
fileLogo.PostedFile.SaveAs(Server.MapPath(@"TempFiles/" + fileName));//TempFiles is folder , you have to create its for temporary period.
imgLogo.ImageUrl = @"TempFiles/" + fileName;
hdnUploadedLogo.Value = @"TempFiles/" + fileName;
}
//upload image to a permanent folder at the time of save.
private string UploadImage(string countryId)
{
string galleryPath = string.Empty;
string mappedPath = string.Empty;
if (hdnUploadedLogo.Value != "")
{
FileInfo tempfile = new FileInfo(Server.MapPath(hdnUploadedLogo.Value));
mappedPath = Server.MapPath(@"../Repository/ImageGallery") + @"\" + countryId;
if (!Directory.Exists(mappedPath))
Directory.CreateDirectory(mappedPath);
if (Directory.Exists(mappedPath + @"\" + tempfile.Name))
{
FileInfo oldfile = new FileInfo(mappedPath + @"\" + tempfile.Name);
oldfile.Delete();
}
tempfile.MoveTo(mappedPath + @"\" + tempfile.Name);
galleryPath += "../Repository/ImageGallery/" + countryId + "/" + tempfile.Name;
}
else
galleryPath = hdnLogoPath.Value;
return galleryPath;
}
in case of thumbnail one ex is
ImagePath = "";
ThumbnailPath = "";
string imageMappedPath = "";
string thumbnailMappedPath = "";
if (hdnUploadedContent.Value != "")
{
FileInfo tempfile = new FileInfo(Server.MapPath(hdnUploadedContent.Value));
imageMappedPath = Server.MapPath("../Repository/EventGallery/") + Id + "/Images/";
thumbnailMappedPath = Server.MapPath("../Repository/EventGallery/") + Id + "/Thumbnails/";
if (!Directory.Exists(imageMappedPath))
Directory.CreateDirectory(imageMappedPath);
if (!Directory.Exists(thumbnailMappedPath))
Directory.CreateDirectory(thumbnailMappedPath);
System.Drawing.Image uploadedImage = System.Drawing.Image.FromFile(Server.MapPath(hdnUploadedContent.Value));
System.Drawing.Image image = GlobalOperations.ResizeImage(uploadedImage, new System.Drawing.Size(400, 300));
System.Drawing.Image thumbnail = GlobalOperations.ResizeImage(uploadedImage, new System.Drawing.Size(100, 100));
image.Save(imageMappedPath + tempfile.Name);
thumbnail.Save(thumbnailMappedPath + tempfile.Name);
ImagePath += "~/Repository/EventGallery/" + Id + "/Images/" + tempfile.Name;
ThumbnailPath += "~/Repository/EventGallery/" + Id + "/Thumbnails/" + tempfile.Name;
uploadedImage.Dispose();
File.Delete(Server.MapPath(hdnUploadedContent.Value));
}
else
{
ImagePath = hdnContentImagePath.Value.ToString();
ThumbnailPath = hdnThumbnailPath.Value.ToString();
}