0

Good day, I am working in asp page for upload image file and preview it by using JavaScript code and I want to add 'remove button' that delete image from (id="imgpreview") and file path ( ID="FileUploadControl")

My code

function showpreview(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
      $('#imgpreview').attr('src', e.target.result);
    }
    reader.readAsDataURL(input.files[0]);
  }
}

function showImage() {
  var add = document.createElement('img');
  add.src = ''
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <asp:Label ID="Label5" runat="server" Text="Select File to Upload"></asp:Label>
    </td>
    <td>
      <asp:FileUpload ID="FileUpload1" runat="server" onchange="showpreview(this);" />
      <asp:Label ID="Label4" class="stylelbl" runat="server" Text="File Type : JPG, Max File Size : 500kb">
      </asp:Label>
    </td>
  </tr>
  <tr>
    <td>
      <img id="imgpreview" alt="" src="" />
    </td>
  </tr>
</table>
1
  • What is showImage supposed to do? Commented Nov 19, 2020 at 7:35

1 Answer 1

1

from your code already add image in javascript, so you may add "remove image" button in your html / aspx.

<input type='button' onclick='removeImg();' value='remove image'>

javascript :

function removeImg() {
    document.getElementById('imgpreview').src = '';
}

You may check on your code-behind "if" the image was removed.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Flash for your help, your code is work only remove 'imgpreview' i want to remove image path from 'FileUpload1'.
For 'edit' fileupload, you may refer to this : <stackoverflow.com/questions/19060378/…>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.