1
public IFormFile UploadedImage { get; set; }
        <div class="form-group row">
            <label asp-for="UploadedImage" class="ml-2">Image</label>
            <input asp-for="UploadedImage" class="form-control" type="file"/>
            <span asp-validation-for="UploadedImage" class="text-danger"></span>
        </div>

Currently user can upload anything, which will lead to awful things in backend.

Is there any easy straightforward way to allow uploading only images? Looking online some solutions look absolutely terrifying, but they are 5+ years old, so maybe .NET got better way to do it. Like [Image] attribute or something.

1
  • 2
    I think this question has already been asked and has an answer here: link
    – Marko
    May 15, 2021 at 0:02

1 Answer 1

1

You can try to use accept attribute. Here is a demo:

View:

 <div class="form-group row">
                <label asp-for="UploadedImage" class="ml-2">Image</label>
                <input asp-for="UploadedImage" class="form-control" type="file"  accept="image/png, image/jpeg, image/gif"/>
                <span asp-validation-for="UploadedImage" class="text-danger"></span>
    </div>

When you click choose file,you can only see and select with .png,.jpeg,.gif type file.

Here is my files:

enter image description here

And when I click choose files:

enter image description here

Your Answer

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