Hi ashish007,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Button ID="btnFileUpload" Text="Select File" runat="server" OnClick="Upload" />
<asp:FileUpload ID="FileUpload1" runat="server" Style="display: none" />
<br />
<asp:TextBox runat="server" ID="txtImage" TextMode="MultiLine" Height="120px" Width="120px" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var fileupload = $("#FileUpload1");
var button = $("#btnFileUpload");
button.click(function () {
fileupload.click();
});
fileupload.change(function () {
var fileName = $(this).val().split('\\')[$(this).val().split('\\').length - 1];
filePath.html(fileName);
});
});
</script>
Code
C#
protected void Upload(object sender, EventArgs e)
{
decimal size = Math.Round(((decimal)FileUpload1.PostedFile.ContentLength / (decimal)1024 / (decimal)1024), 2);
if (size <= 5)
{
FileUpload1.SaveAs(Server.MapPath("~/") + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName));
txtImage.Attributes.Add("style", "background-repeat: no-repeat; background-image:url('" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName) + "')");
}
}
VB.Net
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim size As Decimal = Math.Round((CDec(FileUpload1.PostedFile.ContentLength) / CDec(1024) / CDec(1024)), 2)
If size <= 5 Then
FileUpload1.SaveAs(Server.MapPath("~/") & System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName))
txtImage.Attributes.Add("style", "background-repeat: no-repeat; background-image:url('" & System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName) & "')")
End If
End Sub
Screenshot