0

I want to make a function that the user can choose a text file from a file picker and then it will read the file and do some stuff while reading... I'm having a lot of issues and can't make it work. It does not send back to the controller the correct path only "C:\fakepath\file.txt". I tried putting a real path manually in the function and it said that it can't find the file... I'm really stuck. This is my html file:

 @using (Html.BeginForm("FilterFile", "Home",FormMethod.Post))
{
    <div>
        <input type="file" id="textFile">
    </div>
    <button id="submit-button" type="submit">Filter</button>

    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
}
<script>
        $(document).ready(function () {
            $('form').submit(function () {
                $.ajax({
                    url: '@Url.Action("FilterFile")',
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify({
                        fileName: $('#textFile').val()
                    }),
                    success: function (result) {
                    },
                    error: function (result) {
                        alert("Failed");
                    }
                });
                return false;
            });
        });

This is my controller:

 [HttpPost]
        public JsonResult FilterFile(string fileName)
        {
            var resultMessage = "";
            try
            {
                using (var sr = new StreamReader(fileName))

                {
                    string line;

                    while ((line = sr.ReadLine()) != null)

                    {

                        if (line.Split(' ').Count() > 1)
                        {
                             var number = line.Split(' ')[0];
                            line.Replace(number, "");

                        }

                    }

                }

                resultMessage = "The task was completed successfully!!";
            }
            catch(Exception ex)
            {
                resultMessage = ex.Message;
            }
            return Json(resultMessage);

        }
    }

I would really appreciate if someone can help me since I'm new to programming.Thanks.

BYG
  • 35
  • 7
  • 1
    You can't access the real path from browser due to obvious security issues – charlietfl Jul 19 '18 at 13:03
  • You cannot reliably retrieve the full file path from the client. This is a security feature of browsers. There is absolutely no reason for you to need to know the folder structure of the client anyway. – Rory McCrossan Jul 19 '18 at 13:04
  • but stream reader needs the full path. How do I read the file that the user chose with the filepicker? – BYG Jul 19 '18 at 13:06
  • You will need to receive the data of the file on the server side code, the file name isn't enough to save it. – Wamadahama Jul 19 '18 at 13:08
  • how do I do that? I'm new to this. can you explain how. thanks. – BYG Jul 19 '18 at 13:10
  • For more about C:\fakepath, see [How to resolve the C:\fakepath?](https://stackoverflow.com/q/4851595) – Heretic Monkey Jul 19 '18 at 13:33

1 Answers1

1

Add the HttpPostedFileBase as the parameter recieved by the method. Then get the stream using HttpPostedFileBase.InputStream(). Also don't use the ajax because that is just sending the file name when you want to send the file data.

[HttpPost]
public JsonResult FilterFile(HttpPostedFileBase file)
{
    var resultMessage = "";
    try
    {
        if (file != null && file.ContentLength > 0)
        {
            var stream = file.InputStream();

            using (var sr = new StreamReader(stream))
            {

                string line = "";
                while ((line = sr.ReadLine()) != null)
                {

                    if (line.Split(' ').Count() > 1)
                    {
                        var number = line.Split(' ')[0];
                        line.Replace(number, "");

                    }

                }

            }
        }
        resultMessage = "The task was completed successfully!!";
    } catch (Exception ex) {
        resultMessage = ex.Message;
    }
    return Json(resultMessage);
}
Wamadahama
  • 1,489
  • 13
  • 19