[英]list down the files in a folder in asp.net mvc
I want to list all the files in folder once page loading . 我想在页面加载后列出文件夹中的所有文件。 so 所以
For that I just created like this 为此,我只是这样创建的
HTML code HTML代码
<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. 但这根本没有列出任何内容,但是一旦调试,我就能看到这个filesinfolder
方法调用并在文件夹中查找文件。
You have a number of errors 您有很多错误
id="idd"
is an <input>
and you cannot append <li>
elements to an <input>
(only a <ul>
element) id="idd"
的元素是<input>
并且不能将<li>
元素附加到<input>
(仅<ul>
元素) JsonRequestBehavior.AllowGet
so nothing will be returned 您的ajax调用正在进行GET调用,但是您尚未包含JsonRequestBehavior.AllowGet
因此将不会返回任何内容 List<FileInfo>
but accessing the Url
property which dos not exist. 您尝试返回List<FileInfo>
但是访问不存在的Url
属性。 FullName
property, creating <a href="http://C:/filePath.someFileName.xls
would not navigate to the file on the server 即使您要访问(例如) FullName
属性,创建<a href="http://C:/filePath.someFileName.xls
也不会导航到服务器上的文件 Since it appears you only want the name of the file, then your code should be 由于它似乎只需要文件名,因此您的代码应为
Html HTML
<ul id="filelist"></ul>
Script 脚本
$(document).ready(function () {
$.ajax({
url: '@Url.Action("filesinfolder", "Home")', // don't hardcode
dataType: "json",
success: function (data) {
$.each(data, function (index, item) {
$('#filelist').append($('<li></li>').text(item));
});
},
error: function () {
}
});
});
Note the contentType
option is not required 注意, contentType
选项不是必需的
Controller 控制者
public JsonResult filesinfolder()
{
string salesFTPPath = "C:/filePath";
DirectoryInfo salesFTPDirectory = new DirectoryInfo(salesFTPPath);
IEnumerable<string> files = salesFTPDirectory.GetFiles()
.Where(f => f.Extension == ".xls" || f.Extension == ".xml" || f.Extension == ".jps" || f.Extension == ".jpg" || f.Extension == ".jpeg" || f.Extension == ".png")
.OrderBy(f => f.Name)
.Select(f => f.FullName);
return Json(files, JsonRequestBehavior.AllowGet);
}
If you're using GET request then you need to explicitly allow get requests on JSON results. 如果使用的是GET请求,则需要显式允许对JSON结果进行get请求。
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(), JsonRequestBehavior.AllowGet);
}
Also, consider returning just the data you need to your view. 另外,考虑只将所需的数据返回给视图。 The FileInfo
object is complex and might not be easily serialized. FileInfo
对象很复杂,可能不容易序列化。
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.