1

I have an ASP.NET Core 5 web app with an action to upload a file.

This is de model:

public class UploadViewModel
{
    public IFormFile FileToUpload { get; set; }
}

This is the view part

<div class="form-group">
    <label class="control-label">@Localizer["FileToUpload"]</label>
    <input asp-for="FileToUpload" class="form-control" />
    <span asp-validation-for="FileToUpload" class="text-danger"></span>
</div>

In the web browser I can see a "Choose file" button inside a text box with "No file chosen" text:

Choose file view example

This app is localized so I need to change these texts depending on the selected language, is it possible?

Jon
  • 891
  • 13
  • 32
  • You can refer to the [link](https://stackoverflow.com/questions/5138719/change-default-text-in-input-type-file),but if you change the button text with the solution in the link,`"No file chosen"` will not shown. – Yiyi You Sep 28 '21 at 05:34

1 Answers1

1

Maybe it's not possible using just c#. but you can do this just using Css and Javascript.

<div class="form-group">
    
    <input type='file' id="aa" onchange="pressed()" asp-for="FileToUpload" class="form-control" />
    <label id="fileLabel" class="control-label">@Localizer["FileToUpload"]</label>
   ...
</div>

Javascript

window.pressed = function(){
    var a = document.getElementById('aa');
    if(a.value == "")
    {
        fileLabel.innerHTML = "Choose file";
    }
    else
    {
        var theSplit = a.value.split('\\');
        fileLabel.innerHTML = theSplit[theSplit.length-1];
    }
};

CSS

input[type=file]{
    width:90px;
    color:transparent;
}

Output

enter image description here

Or you can do this by using pure Css like below:-

<input type='file' id="foo" asp-for="FileToUpload" class="form-control" />

CSS

input[type=file]{
 color:transparent;
}
input[type=file]:after {
    color: #000;
    content:"This Works Man!!";
}

Output:-

enter image description here

Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26