繁体   English   中英

C#-服务器端WebMethod向客户端返回大字节[]作为null

[英]C# - Server side WebMethod returns large byte[] as null to client

我有一个服务器端ASP.NET WebMethod(System.Web.Services.WebMethod)。 它是通过JavaScript从客户端调用的。 它使用System.Net.WebClient通过其URL下载SSRS报告,将报告转换为字节数组,然后将其传递回客户端。 对于不超过11​​08.9998字节的报表,此方法很好用,但是超出此范围的任何内容(例如11089999字节)都会向JavaScript返回空值。 我需要返回一个11,711,711字节的报告,并且可能会增加,但最多不超过13 MB。

在Web.config中,我有:

<system.web>
    <httpRuntime targetFramework="4.5" maxRequestLength="2097151" executionTimeout="3600"/>
</system.web>

<system.web.extensions>
  <scripting>
      <webServices>
          <jsonSerialization maxJsonLength="2147483647" />
      </webServices>
  </scripting>
</system.web.extensions>

这是WebMethod中的相关行:

 [System.Web.Services.WebMethod]
 public static byte[] DownloadReport(string opt, string type, string arg)
 {
     byte[] report = new byte[0];

     // Misc code to get the SSRS URL

     using (System.Net.WebClient client = new System.Net.WebClient())
     {
         client.UseDefaultCredentials = true;
         byte[] data = client.DownloadData(url);
         string path = HttpContext.Current.Server.MapPath(fileName);
         File.WriteAllBytes(path, data);

         using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read))
         {
             report = new byte[fs.Length];
             fs.Read(report, 0, (int)fs.Length);
         }
         File.Delete(path);
     }
     return report;
 }

这是JavaScript PageMethod调用和回调:

 PageMethods.DownloadReport(opt, type, arg, DownloadReportCallback, ReportErrors);

 function DownloadReportCallback(results) {
    var bytes = new Uint16Array(results.length);
    for (var i = 0; i < results.length; i++) {
        bytes[i] = results[i];
    }
    var file = new Blob([bytes], { type: mime });
    // For IE10+
    if (window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(file, name);
    }
    // For other browsers.    
    else {
        var a = document.createElement("a"),
        url = URL.createObjectURL(file);
        a.href = url;
        a.download = name;
        document.body.appendChild(a);
        a.click();
        setTimeout(function () {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);
        }, 0);
     }
  }

我已经尝试了很多次,但似乎无法使它适用于大于11,089,998字节的报告。 有人有什么想法吗? 感谢您的时间。

我有一些实例,其中webconfig无法正常工作,我需要在该方法的序列化程序上对其进行设置。

JavaScriptSerializer serializer = new JavaScriptSerializer();

serializer.MaxJsonLength = Int32.MaxValue; 

return serializer.Serialize(response);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM