日期:2014-05-17  浏览次数:20422 次

让文件直接在浏览器打开

我现在是点击,直接下载。    我想再做一个,点击能够在浏览器打开,因为 谷歌浏览器 可以直接打开 PDF文件,可以直接浏览




        public ActionResult GetFile(string id)
        {
            //var dl = DownloadsDal.GetById(id);
            var book = LibraryBLL.GetBook(id);
            if (!(bool)book.CanDownload)
                return Content(ClientScript.AlertAndBack("亲,这样做,不对哦!!!")); //防止用户输入超链接下载

            if (book == null)
                return JavaScript(ClientScript.AlertAndBack("对不起,不存在指定的下载项目"));

            StringBuilder sb = new StringBuilder("~" + book.VirDir).Replace("\\","/").Append(Constants.BookName).Append(book.Extension);
            string filePath = HttpContext.Server.MapPath(sb.ToString());
            
            if (!System.IO.File.Exists(filePath))
                return Content(ClientScript.AlertAndBack("对不起,要下载的文件不存在,该文件可能已被删除或改名"));

            FilePathResult fpr = File(filePath, "application/octet-stream", book.Title);
            LibraryBLL.UpdateDownloadTimes(id);                                         //更新下载次数
            return fpr;
        }