Can someone tell me how can i get list of files on a web directory using mvc 4 razor web api and use that api in another project to list files in a gridview or anything else.
Basically I am developing a Web Api which returns a list of files on a given diretory on the web server and using that Api in another project which is choosed from Internet template in the ASP.net MVC4 Razor, I want to get available files in the directory
using that Api call and display those files in a gridview where user can choose which file to use for further task.
I need urgent help on this scenario as I am very new to MVC and Razor is totally new for me.
There is no ready to use grid view in MVC, you can try using a jQuery grid plugin or any other MVC plugins (there are plenty available)
Now in you WebAPI controller you can use the below code to get all files in you server folder.
DirectoryInfo directory = new DirectoryInfo(Server.MapPath(@"~\FolderPassedToMethod"));
var files = directory.GetFiles().ToList();
return files;
Now in you razor code you can use the below code to display the data as a table. The getJSON can be called on document.ready jQuery event or on any click event
$.getJSON('[URL]', function(data) {
var fileTable = $("#tbFiles");
$.each(data, function() {
var row = $('$(this).Name$(this).FullName');
$("#tbFiles").append(row );
});
});
There is no ready to use grid view in MVC, you can try using a jQuery grid plugin or any other MVC plugins (there are plenty available)
Now in you WebAPI controller you can use the below code to get all files in you server folder.
DirectoryInfo directory = new DirectoryInfo(Server.MapPath(@"~\FolderPassedToMethod"));
var files = directory.GetFiles().ToList();
return files;
Now in you razor code you can use the below code to display the data as a table. The getJSON can be called on document.ready jQuery event or on any click event
$.getJSON('[URL]',function(data){ var fileTable = $("#tbFiles"); $.each(data,function(){ var row = $('$(this).Name$(this).FullName'); $("#tbFiles").append(row ); }); });
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Net.Http;
using System.Web.Http;
namespace MyWebApiController.Controllers.Controllers
{
public class DirectoryController : ApiController
{
// GET /api/directory
public IEnumerable<string> Get()
{
//HttpContext.Current.Request.MapPath
//DirectoryInfo directory = new DirectoryInfo((""));
return new string[] { "file1", "file2" };
}
// GET /api/directory/5
public string Get(string id)
{
return "filename";
}
// POST /api/directory
public void Post(string value)
{
}
// PUT /api/<controller>/5
public void Put(int id, string value)
{
}
atulrungta
0 Points
6 Posts
Get list of files on a web directory using mvc 4 razor web api and use that api in another projec...
Apr 23, 2012 06:32 AM|LINK
Hello,
Can someone tell me how can i get list of files on a web directory using mvc 4 razor web api and use that api in another project to list files in a gridview or anything else.
Basically I am developing a Web Api which returns a list of files on a given diretory on the web server and using that Api in another project which is choosed from Internet template in the ASP.net MVC4 Razor, I want to get available files in the directory using that Api call and display those files in a gridview where user can choose which file to use for further task.
I need urgent help on this scenario as I am very new to MVC and Razor is totally new for me.
Thanks in Advance,
Atul Rungta
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: Get list of files on a web directory using mvc 4 razor web api and use that api in another pr...
Apr 23, 2012 07:55 AM|LINK
Directory.GetFiles
gopakumar.r
Participant
959 Points
193 Posts
Re: Get list of files on a web directory using mvc 4 razor web api and use that api in another pr...
Apr 23, 2012 07:56 AM|LINK
There is no ready to use grid view in MVC, you can try using a jQuery grid plugin or any other MVC plugins (there are plenty available)
Now in you WebAPI controller you can use the below code to get all files in you server folder.
Now in you razor code you can use the below code to display the data as a table. The getJSON can be called on document.ready jQuery event or on any click event
$.getJSON('[URL]', function(data) { var fileTable = $("#tbFiles"); $.each(data, function() { var row = $('$(this).Name$(this).FullName'); $("#tbFiles").append(row ); }); });Hope this helps you
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
atulrungta
0 Points
6 Posts
Re: Get list of files on a web directory using mvc 4 razor web api and use that api in another pr...
Apr 23, 2012 08:08 AM|LINK
Thanks for your help. I was looking at this article http://www.codeproject.com/Articles/344078/ASP-NET-WebAPI-Getting-Started-with-MVC4-and-WebAP to get started with web api. But unable to find out where to use your code exactly.
atulrungta
0 Points
6 Posts
Re: Get list of files on a web directory using mvc 4 razor web api and use that api in another pr...
Apr 23, 2012 09:33 AM|LINK
Server.MapPath does not work in this scenario:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Net.Http;
using System.Web.Http;
namespace MyWebApiController.Controllers.Controllers
{
public class DirectoryController : ApiController
{
// GET /api/directory
public IEnumerable<string> Get()
{
//HttpContext.Current.Request.MapPath
//DirectoryInfo directory = new DirectoryInfo((""));
return new string[] { "file1", "file2" };
}
// GET /api/directory/5
public string Get(string id)
{
return "filename";
}
// POST /api/directory
public void Post(string value)
{
}
// PUT /api/<controller>/5
public void Put(int id, string value)
{
}
// DELETE /api/<controller>/5
public void Delete(int id)
{
}
}
}
atulrungta
0 Points
6 Posts
Re: Get list of files on a web directory using mvc 4 razor web api and use that api in another pr...
Apr 25, 2012 04:38 AM|LINK
Thanks Gopakumar.r
Your solution helped me a lot
I did follwing things:
In the API Controller class i wrote
public IEnumerable<string> Get() { string dirPath = Path.Combine(ConfigurationManager.AppSettings["MyFilesPath"].ToString()); List<string> files = new List<string>(); DirectoryInfo dirInfo = new DirectoryInfo(dirPath); foreach (FileInfo fInfo in dirInfo.GetFiles()) { files.Add(fInfo.Name); } return files.ToArray(); }And in My Razor .cshtml page I wrote:
<script type="text/javascript"> $.getJSON('api/filelist', function (data) { var fileList = $("#files"); $.each(data, function (key, val) { var row = ('<li>' + val +'</li>'); $("#files").append(row); }); }); </script> <ul id="files"> </ul>This works fine for me.