0

I'm working on a Javascript compiler, first of all I want to load the text.txt by opening a File Dialog and load this into my Textbox. I've found some documentation on the internet how to achieve this but most of these contents teaches you by saving the file into your server. I'm looking how to just load the text file and paste or show into a Textbox.

This is my code which I have achieved so far:

Model

    public class CargarArchivo
    {
        public string file { get; set; }
    }

Controller

    [HttpPost]
    public ActionResult Index( HttpPostedFileBase File)
    {
        CargarArchivo archivo = new CargarArchivo();

        return View(archivo);
    }

View

 @using (Html.BeginForm())
{

    @Html.EditorFor(model => model.file, new { htmlAttributes = new { @class = "form-control", @type = "file" } })

    <input type="submit" value="upload" class="btn btn-primary" />
}

I have no idea what is HttpPostedFileBase for, I just followed this on the documentation. Code works OK and just open a file dialog.

I have tried this, but didn't work:

 [HttpPost]
    public ActionResult Index( string File)
    {
        var cont = System.IO.File.ReadAllText(File);
        CargarArchivo archivo = new CargarArchivo();
        archivo.File = cont;

        return View(archivo);
    }
Reyneer Leon
  • 356
  • 2
  • 14
  • If the file resides on the user's computer see here https://stackoverflow.com/questions/31746837/reading-uploaded-text-file-contents-in-html – Jasen May 01 '19 at 01:08
  • If the file resides on he user's computer means I should use JS? Good idea, but I thought ASP.NET MVC could handle this *client side* load. – Reyneer Leon May 01 '19 at 01:19
  • ASP.NET is server-side processing. You'd need to upload the file to the server before you can leverage that API then return the rendered view to the browser. All of that is unnecessary if you don't need to store the file server-side then return a completed form back to the user. – Jasen May 01 '19 at 02:24
  • do you want select file and display content immediately? – Hien Nguyen May 01 '19 at 04:33
  • That's right Hien, but I got this by typing some JS code, thanks to Jasen. – Reyneer Leon May 01 '19 at 05:05

0 Answers0