0

在我看来,我需要从本地文件夹下载文件。

所以我写了一个 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})

我可以成功下载文件我如何下载图片上的文件点击

请帮忙

4

1 回答 1

0

您可以使用 window.open("url to file") 并下载文件。url 可以是控制器上的操作方法。

于 2012-06-08T12:14:26.133 回答