0
在我看來,我需要從本地文件夾下載文件。在mvc3中下載文件的問題
所以我寫了一個Ajax功能像下面
<img id="trailer" src="../../Images/icon.gif" alt="exists" title="Click on image to Download file"/>
<script type="text/javascript">
$(document).ready(function() {
$('#trailer').click(function() {
var cid = $('#CourseID').val();
var fnm = $('#FileName').val();
var url2 = "/Coursework/DownloadPrtfTrailor" + '?FileName=' + fnm + '&CourseID=' + cid;
$.ajax({
url: url2,
cache: false,
type: 'POST'
});
});
});
</script>
而且在控制我寫了如下因素代碼:
[HttpPost]
public ActionResult DownloadPrtfTrailor()
{
string fileName = string.Empty;
string courseID = string.Empty;
string filepath = string.Empty;
if (Request.QueryString != null && Request.QueryString.Count > 0)
{
if (!string.IsNullOrEmpty(Request.QueryString["FileName"]))
fileName = Request.QueryString["FileName"];
if (!string.IsNullOrEmpty(Request.QueryString["CourseID"]))
courseID = Request.QueryString["CourseID"];
}
try
{
var fs = System.IO.File.OpenRead(Server.MapPath("/ePortfolio/" + courseID + "/" + "Icons" + "/" + fileName));
string extn = "application/" + Path.GetExtension(fileName);
return File(fs, extn, fileName);
}
catch
{
throw new HttpException(404, "Couldn't find " + fileName);
}
}
通過這個我無法下載文件
但當我通過下面的動作鏈接調用函數時
@Html.ActionLink("Download", "DownloadPrtfTrailor",
new {CourseID=item.prtfMaster.CourseID, fileName1 = item.prtfMaster.IconFileName})
我可以成功地下載我如何下載圖像文件的文件 點擊
請幫助
以及我試過這....但不工作...因爲我沒有得到窗口要求「保存」或「打開」 – priya77