Search code examples
c#asp.netwebmethodpagemethodsmaxrequestlength

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


I have a server-side ASP.NET WebMethod (System.Web.Services.WebMethod). It is called from the client via JavaScript. It uses System.Net.WebClient to download an SSRS report via its URL, converts the report to a byte array and passes it back to the client. This works fine for reports that are up to 11,089,998 bytes, but anything over that (say, 11,089,999 bytes) returns a null value to the JavaScript. I need to return a report that is 11,711,711 bytes, and will probably increase, but up to no more than 13 MB.

In the Web.config, I have:

<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>

Here are the relevant lines from the 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;
 }

And here is the JavaScript PageMethod call and callback:

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

I have tried and tried but can't seem to get this to work for reports larger than 11,089,998 bytes. Does anybody have any ideas? Thank you for your time.


Solution

  • I've had instances where the webconfig didn't work and I needed to set it on the serializer in the method.

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    
    serializer.MaxJsonLength = Int32.MaxValue; 
    
    return serializer.Serialize(response);