Error displaying image preview before uploading

angular 6 multiple image upload with preview
angular 8 image upload with preview
file upload with preview in angular 5
angular 6 file upload with preview
angular 7 image upload with preview
angular 6 image upload with preview
angular-file-upload-preview
angular 4 image upload with preview

I am using following code to upload image from multiple inputs and displaying a preview. I can view preview of first input only. When it comes to other three it does not work.

SCRIPT

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

HTML

<input type='file' onchange="readURL(this);" />
<img id="blah" src="http://placehold.it/180" alt="your image" />
<input type='file' onchange="readURL(this);" />
<img id="blah2" src="http://placehold.it/180" alt="your image" />
<input type='file' onchange="readURL(this);" />
<img id="blah3" src="http://placehold.it/180" alt="your image" />
<input type='file' onchange="readURL(this);" />
<img id="blah4" src="http://placehold.it/180" alt="your image" />

NOTE: Is there a way without changing HTML, and making some small change in script how to display preview.

I am using Bootstrap v3.3

Here is what you can do without changing the HTML. Using the nextElementSibling you can get hold of the next element and assign the src value to it.

Alternetive is with jQuery's .next() function that those the same thing.

$(input).next().attr('src', url);

I'm also suggesting using the Object urls instead of reading the file as dataURL which takes time to compile and decompile back and forth between a base64 encoded string where as a object url is just a pointer to a file.

function readURL(input) {
  if (input.files && input.files[0]) {
    var url = URL.createObjectURL(input.files[0]);
    input.nextElementSibling.src = url;
    // $(input).next().attr('src', url);
  }
}

If you could, maybe add a extra attribute to the file input accept="image/*" so they will only select a valid image file

Image preview before upload in angular 5, Which works just fine and displays the image preview. The problem I originally faced was that I receeived the below error in the chrome  How to display Preview of an image before upload in Asp.Net C#3.5. Hi I am using FileUpload control to upload an image. I am displaying preview of an image when

actually issue is that you use

$('#blah')

so every time you update only one image tag, you need to target the diff img tag accordingly, one way to do this is

Html

<input type='file' onchange="readURL(this, 'blah');" />
 <img id="blah" src="http://placehold.it/180" alt="your image" />
 <input type='file' onchange="readURL(this, 'blah2');" />
 <img id="blah2" src="http://placehold.it/180" alt="your image" />
 <input type='file' onchange="readURL(this, 'blah3');" />
 <img id="blah3" src="http://placehold.it/180" alt="your image" />
  <input type='file' onchange="readURL(this, 'blah4');" />
  <img id="blah4" src="http://placehold.it/180" alt="your image" />

now you get diff id of img tag every time now you js function look like.

JS

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

now it works, good luck

Error displaying image preview before uploading, Here is what you can do without changing the HTML. Using the nextElementSibling you can get hold of the next element and assign the src  Show an image preview before uploading using Angular 7 December 3, 2018 November 30, 2018 Talking Dotnet Angular I am currently working on an Angular 7 based app and as part of one of the requirements, the image preview needs to be shown in the app before uploading the actual image and the file upload control is only allowed to select images.

You need to add jQuery in your html file

function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function (e) {
      $('#blah').attr('src', e.target.result);
    };
    reader.readAsDataURL(input.files[0]);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" onchange="readURL(this)"/>
<img src="#" id="blah" />

Create Thumbnail Preview of Images Using HTML5 API, Which API can be used to display thumbnail image of a file being uploaded in a html5 photo sharing website? We can select any image from user computer and display image in image control for preview before upload. By using upload and save button user can save image to server folder through internet.

Error Message: There is a Problem Uploading Your Image, Sometimes these messages show up while adding an image from your computer: "An unexpected error occurred while uploading your image, please try and reduce it using your image editor before trying to upload it again. Image preview is a great feature for user to check there image before upload whether the correct image is going to upload or if the image looks nice or not etc.So in this tutorial we will show you how to preview image before upload using JavaScript,HTML and CSS.You may also like upload multiple images with preview using jQuery and PHP.

Previewing Image Before File Upload - JavaScript Tutorial, in order to display a preview of an image before uploading it to the web how do i Duration: 14:40 Posted: Jul 25, 2019 User experience can be greatly improved on an image upload feature if we enable the user to preview the image they have selected before actually uploading it to the server by clicking the upload button. In this tutorial, we will create a form that takes two inputs: the user's profile picture (image), and their bio (text).

How to Validate image width and height before upload using , How to Validate Image width and height Before upload using Create an file element and a <div > container with <img > to show image preview after upload. $returnText = "Sorry, there was an error uploading your file. Client Side Implementation for displaying live preview of image before upload Firstly we need to check whether the file is a valid image file. Then we need to determine the browser and its version as based on that we need to select the appropriate way to display the image preview before upload.

Comments
  • can you create a fiddle