如何在ASP.NET MVC中将文本文件加载到文本框中?

时间:2019-05-01 00:13:23

标签: asp.net-mvc

我正在使用Javascript编译器,首先,我想通过打开文件对话框来加载 text.txt 并将其加载到我的文本框中。我在互联网上找到了一些文档来实现这一目标,但是其中大多数内容都是通过将文件保存到服务器中来教您的。我正在寻找如何仅加载文本文件并将其粘贴或显示在文本框中的方法。

这是我到目前为止已经实现的代码:

模型

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

控制器

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

        return View(archivo);
    }

查看

 @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" />
}

我不知道HttpPostedFileBase的含义,我只是在文档中遵循了这一点。代码可以确定,并且只需打开一个文件对话框即可。

我已经尝试过了,但是没有用:

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

        return View(archivo);
    }

0 个答案:

没有答案