Recently I have been developing web form application in ASP.NET (c#): I have an Image control:
<asp:Image ID="Avatar" runat="server" Height="225px" ImageUrl="~/Images/NoUser.jpg" Width="225px" />
And FileUpload & Button control
<asp:FileUpload ID="avatarUpload" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
When user click button then "Upload" code is executed (the image is sent to the database). Problem is that I like to display the image which the user selected in Avatar Image controller before the user clicks "desperate" button.
Is that possible to do this automatically?
With the help of
File Api
ofHTML5
(Example: Using files from web applications) you can accomplish this easily. Change the markup to useinput type="file"
instead ofasp:FileUpload
and add ID, add tagrunat="server"
to make it accessible from server. Your markup should look like:Now add a javascript function
previewFile
in the head of document:Now after selecting an image you can see the preview like below:
You can use css to re-size it to a thumbnail. After clicking the Upload button, in the code you can find the posted file: