Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
ASP.NET Core Preview Browsed Image

I found an answer here, but it uses a JavaScript. I am trying to apply this script but in razor codes but I don't know how to do it, I searched/googled with different keyword but all answers don't use a razor code. Can anyone help me ?

Here is a part of the cshtml page.

    <tr>
        <td><label>Image</label></td>
        <td>
            <script>
                function ImgPre(input) {
                    if (input.files[0]) {
                        var uploadImg = new FileReader();
                        uploadImg.onload = function (displayImg) {
                            $("#imagePreviewer").attr('src', displayImg.target.result);
                        }
                        uploadImg.readAsDataURL(input.files[0]);
                    }
                }
            </script>

            <input type="file" accept="image/*" asp-for="Image" onchange="ImgPre(this)" />

            @{
                
                string base64 = "";
                if (Model.Image != null)
                {
                    base64 = Convert.ToBase64String(Model.Image);
                }
                var imgSrc = String.Format("data:image/jpeg;base64,{0}", base64);
            }
            <img id="imagePreviewer" src="@imgSrc" width="150" />

        </td>
    </tr>

how can I write the code in the <script></script> tag to be similar to the one with @{ }

I have been struggling to find a solution but I can't find any. Can someone help me, please ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can write javascript in your cshtml file by surrounding the with @section Scripts{}.

It should looks like

<input type="file" id=" ImgUpload" onchange="ImgPre(this)" />
<button id="butImg">Upload</button>
<p>
  <img id="ImgPreview" />
</p>

@section Scripts{
    <script>
        function ImgPre(input) {
          if (input.files[0]) {
            var uploadimg = new FileReader();
            uploadimg.onload = function(displayimg) {
              $("#ImgPreview").attr('src', displayimg.target.result);
            }
            uploadimg.readAsDataURL(input.files[0]);
          }
        }
    </script>
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!