Click here to Skip to main content
15,915,509 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello,

How to use ImageButton for ImageUpload in to the Image Box and ImageSave in to the Server.MapPath using c# asp.net?

Please help me.

Thanks in Advance.

Ankit Agarwal
Website Developer
Posted
Comments
Kornfeld Eliyahu Peter 20-Jan-14 5:20am    
Have you done anything so far?
Let us see you effort (code?)...
[no name] 20-Jan-14 5:22am    
<div class="frant-page-header"><div class="frant-page-header-logo">
<div style="height: 0px; overflow: hidden">
<asp:FileUpload ID="UploadLogo" runat="server" />
</div>
<asp:ImageButton ID="ImageLogo" runat="server" ImageUrl="~/images/64.png" style="width: 100px; height: 100px; line-height: 100px;"
OnClientClick="chooseFile();" />
<script type="text/javascript">
function chooseFile() {
document.getElementById("UploadLogo").click();
}
</script>

protected void ImageLogo_Click(object sender, ImageClickEventArgs e)
{
Boolean FileOK = false;
Boolean FileSaved = false;

if (Upload.HasFile)
{
Session["WorkingImage"] = Upload.FileName;
String FileExtension = Path.GetExtension(Session["WorkingImage"].ToString()).ToLower();
String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (FileExtension == allowedExtensions[i])
{
FileOK = true;
}
}
}

if (FileOK)
{
try
{
Upload.PostedFile.SaveAs(path + Session["WorkingImage"]);

FileSaved = true;
}
catch (Exception ex)
{
LabelWelcome.Text = "File could not be uploaded." + ex.Message.ToString();
LabelWelcome.Visible = true;
FileSaved = false;
}
}
else
{
LabelWelcome.Text = "Cannot accept files of this type.";
LabelWelcome.Visible = true;
}

if (FileSaved)
{
//pnlUpload.Visible = false;
//pnlCrop.Visible = true;

ImageLogo.ImageUrl = "Templates/" + Session["WorkingImage"].ToString();
//uploadImage.ImageUrl = "Templates/" + Session["WorkingImage"].ToString();
}

//Session["TempID"] = TextTemplateId.Value.ToString();
}
ZurdoDev 20-Jan-14 11:55am    
Where are you stuck?

1 solution

I did not write it, but this[^] apparently uses css to style a button to trigger an upload control. That it's ASP.NET is irrelevant, you need to style the html in the browser.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900