Ajax return files from a directory (Asp.net MVC)

1

I would like to list the files in the form of a link (to click on download).

List the directory files below the upload input

Ajax:

functionCarregaArquivos(Id){$.ajax({url:"/Qualidade/RiscoAcao/ListarArquivos",
        data: { IdAcao: Id },
        async: false,
        success: function (data) {
            $.each(data, function (i, element) {

                console.log(data);

            });
        }
    });
}

C #:

    //Retornar todos os arquivos de um diretório:
        public ActionResult ListarArquivos(int IdAcao)
        {


#if DEBUG
            //Verifica se existe uma pasta com o nome do Id (para salvar dentro dela)
            string caminhoDiretorio = @"//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/" + IdAcao;
#else
                      //Verifica se existe uma pasta com o nome do Id (para salvar dentro dela)
                    string caminhoDiretorio = @"//fileserver/repositorio$/DiagramaProcesso/Producao/AcaoRisco/"+ IdAcao;
#endif

            string[] arquivos = new string[] { };

            if (Directory.Exists(caminhoDiretorio))  // se existe
            {
                arquivos = Directory.GetFiles(caminhoDiretorio);
            }          


            return Json(arquivos, JsonRequestBehavior.AllowGet);
        }

Return (Console.log):

(5) ["//fileserver/repositorio$/DiagramaProcesso/Teste/A…oRisco/107930383d208c14c9429828a041b8eeabb.jpg", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"]0: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/107930383d208c14c9429828a041b8eeabb.jpg"1: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf"2: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf"3: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf"4: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"length: 5__proto__: Array(0)concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}__proto__: Object
riscolista.js:388 (5) ["//fileserver/repositorio$/DiagramaProcesso/Teste/A…oRisco/107930383d208c14c9429828a041b8eeabb.jpg", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"]0: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/107930383d208c14c9429828a041b8eeabb.jpg"1: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf"2: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf"3: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf"4: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"length: 5__proto__: Array(0)concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}__proto__: Object
riscolista.js:388 (5) ["//fileserver/repositorio$/DiagramaProcesso/Teste/A…oRisco/107930383d208c14c9429828a041b8eeabb.jpg", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"]0: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/107930383d208c14c9429828a041b8eeabb.jpg"1: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf"2: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf"3: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf"4: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"length: 5__proto__: Array(0)
riscolista.js:388 (5) ["//fileserver/repositorio$/DiagramaProcesso/Teste/A…oRisco/107930383d208c14c9429828a041b8eeabb.jpg", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"]0: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/107930383d208c14c9429828a041b8eeabb.jpg"1: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf"2: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf"3: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf"4: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"length: 5__proto__: Array(0)concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}__proto__: Object
riscolista.js:388 (5) ["//fileserver/repositorio$/DiagramaProcesso/Teste/A…oRisco/107930383d208c14c9429828a041b8eeabb.jpg", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf", "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"]0: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/107930383d208c14c9429828a041b8eeabb.jpg"1: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (1).pdf"2: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList (2).pdf"3: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\CustomerList.pdf"4: "//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/10793\EmailHandler.zip"length: 5__proto__: Array(0)concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}__proto__: Object

    
asked by anonymous 31.07.2018 / 13:58

1 answer

1

JS:

 function CarregaArquivos(Id) {
        $("#tblAcao > tbody").empty();
    $.ajax({
        url: "/Qualidade/RiscoAcao/ListarArquivos",
        data: { IdAcao: Id },
        async: false,
        success: function (data) {

            var caminho = "";
            var nome = "";

            //Verifica se retorna os anexos
            if (data.length == 0) {
                $("#tblAcao > tbody").append(
                    "<tr>" +
                    "  <th>SEM ANEXOS</th>" +
                    "</tr>"
                );
            }
            else
            {
                //Tem anexo
                $.each(data, function (i, element) {

                    $("#tblAcao > tbody").append(
                        "<tr>" +
                        "  <th><a href='/Qualidade/RiscoAcao/Download?caminho=" + element.Caminho + "&nomeArquivo=" + element.Nome + "'>" + element.Nome + "</a></th>" +                    
                        "</tr>"
                    );
                });
            }
        }
    });
    }

HTML:

  <table id="tblAcao" class="table table-striped" border="1">
                    <thead>
                        <tr>
                            <th>ARQUIVOS ANEXADOS</th>
                        </tr>
                    </thead>
                    <tbody>                     
                    </tbody>
                </table>

ASP.NET Controller

 public ActionResult ListarArquivos(int IdAcao)
        {        

#if DEBUG
            //Verifica se existe uma pasta com o nome do Id (para salvar dentro dela)
            string caminhoDiretorio = @"//fileserver/repositorio$/DiagramaProcesso/Teste/AcaoRisco/" + IdAcao;
#else
                      //Verifica se existe uma pasta com o nome do Id (para salvar dentro dela)
                    string caminhoDiretorio = @"//fileserver/repositorio$/DiagramaProcesso/Producao/AcaoRisco/"+ IdAcao;
#endif
            //Usa a Classe do Model-Entity UploadFileResult
            List<UploadFileResult> lstArquivos = new List<UploadFileResult>();
            DirectoryInfo dirInfo = new DirectoryInfo(caminhoDiretorio);

            // Determine whether the directory exists.
            if (Directory.Exists(caminhoDiretorio))  // se existe
            {
                int i = 0;
                foreach (var item in dirInfo.GetFiles())
                {
                    lstArquivos.Add(new UploadFileResult()
                    {
                        IDArquivo = i + 1,
                        Nome = item.Name,
                        Caminho = dirInfo.FullName + @"\" + item.Name
                    });
                    i = i + 1;
                }
            }
            return Json(lstArquivos, JsonRequestBehavior.AllowGet);
        }
    
31.07.2018 / 18:28