Manipulating TXT files with MVC

Asked

Viewed 157 times

0

I’m developing an MVC app, see below:

Registrorep.text

000185000|3|13072016|2357|12942973988 000185001|3|14072016|0000|127396685 000185002|3|14072016|0800|12519046076 000185003|3|14072016|0800|13303022983 000185004|3|14072016|0800|16140197229 000185005|3|14072016|0800|13136849980 000185006|3|14072016|0801|11988133208

namespace WebAbrirArquivo.Models
{
    public class AbrirArquivo
    {
        public string MeuPis { get; set; }
    }
}
//---------------
namespace WebAbrirArquivo.Controllers
{
    public class AbrirArquivoController : Controller
    {
        public string Conteudo { get; private set; }
        public string StrLinha { get; private set; }
        public string Nsr { get; private set; }
        public string Dtop { get; private set; }
        public string HoraOp { get; private set; }
        public string NPis { get; private set; }

        // GET: AbrirArquivo
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
        {
            IList<string> linesArq = new List<string>();
            IList<String> BuscaQuadro = new List<String>();
            var outputFile = System.IO.File.CreateText(Server.MapPath(@"~/App_Data/Uploads/OutputFile.txt"));
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);                  
                    using (StreamReader reader = new StreamReader(file.FileName))
                    {
                        while (!reader.EndOfStream)
                        {
                           StrLinha = reader.ReadLine();
                            Nsr = StrLinha.Substring(0, 9);
                            HoraOp = StrLinha.Substring(21, 4);
                            NPis = StrLinha.Substring(26, 11);
                            var DtOpC = StrLinha.Substring(12, 2) + "/" + StrLinha.Substring(14, 2) + "/" + StrLinha.Substring(16, 4) +
                            " " + StrLinha.Substring(21, 2) + ":" + StrLinha.Substring(23, 2) + ":" + 0 + 0;
                            linesArq.Add(Nsr + " " + NPis + " " + DtOpC + "\r\n");
                            outputFile.WriteLine(Nsr + " " + NPis + " " + DtOpC);                           
                        }
                    }
                    //  IEnumerable<string> GUARDACOL = linesArq.Where(Nsr => Nsr.Contains("12519046076")); // Valor passado diretamente funciona OK
                      IEnumerable<string> GUARDACOL = linesArq.Where(Nsr => Nsr.Contains(AbrirArquivo.MeuPis)); // Valor da View ERRO:
                    foreach (string NSR in GUARDACOL)
                    {
                        BuscaQuadro.Add(NSR);
                    }

                    // create new txt file
                    var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);
                    file.SaveAs(path);
                }
            }

            ViewBag.linesArq = BuscaQuadro;
            // return RedirectToAction("Upload");
            //if (files.Count() > 0) Console.WriteLine(files.Count()); // display 1
            //if (files.Any()) Console.WriteLine(files.Any()); // display true
            // if (files.First() == null) Console.WriteLine("first null"); // display "first null"
            return View();
        }


        // GET: OpenUpload
        public ActionResult Upload()
        {
            return View();
        }

    }
}
//------------
@model WebAbrirArquivo.Models.AbrirArquivo
@{
    ViewBag.Title = "Index";
}

<h2>Localizar Arquivo</h2>

@using (Html.BeginForm("Upload", "AbrirArquivo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        @Html.LabelFor(model => model.MeuPis, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.MeuPis, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.MeuPis)
        </div>
    </div>
    <p></p> <input name="files" type="file" multiple="multiple" />
    <input type="submit" value="Upload" />
}
<script src="@Url.Content("~/Scripts/methods_pt.js")" type="text/javascript"></script>

//--------------------
@{
    ViewBag.Title = "Upload";
}

<h2>Arquivo Uploaded</h2>

<ul>
    @foreach (string linesArq in ViewBag.linesArq)
    {
        <li>
            @linesArq
        </li>
    }
</ul>

ERRO: Uma referência de objeto é nescessaria para o campo " IEnumerable GUARDACOL = linesArq.Where(Nsr => Nsr.Contains(AbrirArquivo.MeuPis));".

  • you are not getting your model ( Open File ) so it is null.

  • The error is occurring because the Open file instance is null. At no time I see in your code the creation of the object, even setting the property Meupis.

  • Thanks buddy, it all worked out, but, still Peco on the fundamentals.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.