0
votes
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
I think this question has already been asked and has an answer here: linkMarko

1 Answers

0
votes

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