getting error while download the file in mvc2

给你一囗甜甜゛ 提交于 2019-12-13 04:40:54

问题


In my site, i gave download option to download the file. when i am checking in local server it is working properly. But after deploy the server, if i click the link means it will show the following error,

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.

My code here

 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)
             };
         }


     }

This is my code. I don't know what mistake here, Can anyone find my problem and tell me ?


回答1:


The Problem with ur code is that u r missing 'JsonRequestBehavior.AllowGet' while returning json.

  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)
         };
     }


 }


来源:https://stackoverflow.com/questions/24256897/getting-error-while-download-the-file-in-mvc2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!