1

I want to list all the files in folder once page loading . so

For that I just created like this

HTML code

<input id="idd" type="file" multiple="true" class="file" data-preview-file-type="text">

Script

@section scripts{   

 <script type="text/javascript">    

  $(document).ready(function () {
        $.ajax({
            url: '/Home/filesinfolder',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $.each(data, function (index, val) {
                    $('#idd').append('<li><a href="http://'+ val.Url +'" target="_new">' + val.Url + '</a></li>');
                });
            },
            error: function (xhr, status, err) {
                console.log('Response code:' + xhr.status);
                console.log('[Error:' + err + '] ' + status);
            }
        });
    });

</script>

Controller method

    public JsonResult filesinfolder()
    {
        DirectoryInfo salesFTPDirectory = null;
        FileInfo[] files = null;

        string salesFTPPath = "C:/filePath";

        salesFTPDirectory = new DirectoryInfo(salesFTPPath);
        files = salesFTPDirectory.GetFiles();

        files = files.OrderBy(f => f.Name).ToArray();

        var salesFiles = files.Where(f => f.Extension == ".xls" || f.Extension == ".xml" || f.Extension == ".jps" || f.Extension == ".jpg" || f.Extension == ".jpeg" || f.Extension == ".png");

        return Json(salesFiles.ToList());
    }

But this is isn't list down anything at all , but once I debug I can see this filesinfolder method calling and finding files in folder.

kez
  • 2,273
  • 9
  • 64
  • 123
  • What do you get in javascript console ? any errors ? – Ehsan88 Dec 14 '15 at 07:23
  • You cant append `
  • ` elements to an `` Create a `
      ` element and append them to it (and remove the `id` from the input)
  • –  Dec 14 '15 at 07:25
  • @EhsanAbd nothing error at firebug console – kez Dec 14 '15 at 07:25
  • @StephenMuecke I just replace `` with `
      ` but same nothing listdown
      – kez Dec 14 '15 at 07:35
    • You also need `return Json(salesFiles, JsonRequestBehavior.AllowGet);` or add the `method: 'Post"` ajax option (the default is `Get`) And there is no need for the extra overhead of `.ToList()` –  Dec 14 '15 at 07:37
    • But it appears you trying to return a collection of `FileInfo`. Instead you should be returning a collection of `string` containing the property you want to display in the view (but what is `Url` anyway - its not a property of `FileInfo`?) –  Dec 14 '15 at 07:40
    • @StephenMuecke Nope its(`Url`) not a property of `FileInfo` , just want to list down files as links , is this possible to do ? – kez Dec 14 '15 at 07:48
    • But you have `val.Url` in your script (what your returning does not have a property `Url`). Are you just wanting to list the names of all the files? –  Dec 14 '15 at 07:51
    • my plan is list down files as clickable links , but If its impossible to do , I wish to just list the names of all the files – kez Dec 14 '15 at 07:54
    • Yes its possible, but how will you be generating the links? Do you have a controller method that returns a `FileResult` so you can download them? –  Dec 14 '15 at 07:56
    • Nope I don't have controller method to generate the links , Actually I don't want to complicate this , for now just want to list down the files, what should I do now , – kez Dec 14 '15 at 08:00
    • Need a break for a while. I'll add an answer in an hour or so showing how to list the file names, and then we ca expand it later to include links for downloading the actual files. –  Dec 14 '15 at 08:08