【问题标题】:getting error while download the file in mvc2在 mvc2 中下载文件时出错
【发布时间】:2014-07-03 17:59:57
【问题描述】:

在我的网站中,我提供了 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)
             };
         }


     }

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

【问题讨论】:

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


    【解决方案1】:

    您的代码的问题是您在返回 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)
             };
         }
    
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 2014-07-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多