在mvc2中下载文件时出错

时间:2014-06-17 06:18:15

标签: json asp.net-mvc-2 download

在我的网站中,我提供了下载选项来下载该文件。当我在本地服务器上检查它正常工作。但是在部署服务器之后,如果我点击链接意味着它将显示以下错误,

This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.

我的代码

 public ActionResult Download(string fileName)
      {
         string pfn = Server.MapPath("~/Content/Files/" + fileName);
         if (!System.IO.File.Exists(pfn))
         {
             //throw new ArgumentException("Invalid file name or file not exists!");

             return Json(new JsonActionResult { Success = false, Message = "Invalid file name or file not exists!" });
         }
         else
         {

             return new BinaryContentResult()
             {
                 FileName = fileName,
                 ContentType = "application/octet-stream",
                 Content = System.IO.File.ReadAllBytes(pfn)
             };
         }


     }

这是我的代码。我不知道这里有什么错误,有人能找到我的问题并告诉我吗?

1 个答案:

答案 0 :(得分:0)

你的代码问题是你在返回json时缺少'JsonRequestBehavior.AllowGet'。

  public ActionResult Download(string fileName)
  {
     string pfn = Server.MapPath("~/Content/Files/" + fileName);
     if (!System.IO.File.Exists(pfn))
     {
         //throw new ArgumentException("Invalid file name or file not exists!");

         return Json(new JsonActionResult { Success = false, Message = "Invalid file name or file not exists!" },JsonRequestBehavior.AllowGet });
     }
     else
     {

         return new BinaryContentResult()
         {
             FileName = fileName,
             ContentType = "application/octet-stream",
             Content = System.IO.File.ReadAllBytes(pfn)
         };
     }


 }