我需要从物理目录中读取文件并在视图上显示为链接。我正在获取文件的链接,但单击链接时未下载文件。下面是代码。请帮忙。当我将鼠标悬停在链接上时,我可以看到文件为 file:///c:/Test/Test.doc 但是在单击链接时,文件不会被下载。
public class Documents: Controller
{
public ActionResult Documents()
{
return View();
}
//Read the files from directory and display
[HttpGet]
public JsonResult GetDocumentlinks()
{
string data = "";
foreach (string s in Directory.GetFiles(@"C:/Test"))
{
var uri = new Uri(s);
var converted = uri.AbsoluteUri;
data = "</br>" + data + "<a href='" + converted + "'>" + Path.GetFileName(s) + " </a> </br></br>";
}
return Json(data, JsonRequestBehavior.AllowGet);
}
}
<div id="links"></div>
$(document).ready(function () {
LoadTrainingDocsGrid();
});
function LoadTrainingDocsGrid()
{
$.ajax({
cache: false,
url: '../Document/GetDocumentlinks/',
dataType: 'json',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (data) {
debugger;
$('#links').html(data);
}
});
}