title: .net core large file upload method
body: <p>How to upload large files with asp.net core web api? When exceeding 500mb, the following exception message appears. I also set the &quot;COMPlus_gcAllowVeryLargeObjects&quot; environment variable and startup.</p>
  <pre><code>services.Configure&lt;FormOptions&gt;(options =&gt;
      {
          options.BufferBodyLengthLimit = Int64.MaxValue;
          options.MemoryBufferThreshold = Int32.MaxValue;
          options.MultipartBodyLengthLimit = long.MaxValue;
          options.MultipartBoundaryLengthLimit = int.MaxValue;
          options.MultipartHeadersLengthLimit = Int32.MaxValue;
      });
  </code></pre>
  <p>Below is my code. Is there anything I need to set up?</p>
  <pre><code>[HttpPost(&quot;{id}&quot;)] 
  [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
  [DisableRequestSizeLimit]
  public ActionResult UploadLargeFiles(string id, [FromForm]IFormFile files)
  {
      try
      {
          string fileName = files.FileName;
          int fileSize = Convert.ToInt32(files.Length);
  
          var uploadProvider = new JObject();
          var res = new JArray();
  
          var isExistence = _mailService.GetUploadFolder(id);
          if (isExistence != HttpStatusCode.OK)
          {
              var createFolder = _mailService.CreateUploadFolder(id);
              if (createFolder != HttpStatusCode.Created)
              {
                  ModelState.AddModelError(&quot;OneDriveFolderError&quot;, &quot;&quot;);
                  return BadRequest(ModelState);
              }
          }
          if (files.Length &gt; 0)
          {
              byte[] data = new byte[fileSize];
  
              var uploadSessionUrl = _mailService.CreateUploadSession(id, fileName);
              if (uploadSessionUrl != null)
              {
                  uploadProvider = _mailService.UploadByteFile(id, uploadSessionUrl, data, fileName, fileSize);
                  res.Add(uploadProvider);
  
                  Array.Fill(data, (byte)0);
              }
              else
              {
                  ModelState.AddModelError(&quot;sessionFail&quot;, &quot;&quot;);
                  return BadRequest(ModelState);
              }
          }
  
          var Link = this.SaveFileDownloadLink(res);
          return Ok(Link);
      }
      catch (ArgumentNullException e)
      {
          return NotFound(e.Message);
      }
  }
  </code></pre>
  <p>Upload the file to OneDrive in byte format.
  Can a byte hold 2 GB or more?</p>
  <pre><code>public JObject LargeFileUpload(string upn, string url, byte[] file, string fileName, int fileSize)
  {
      int fragSize = 4 * 1024 * 1024; //4MB =&gt; 4 * 1024 * 1024;
      var byteRemaining = fileSize;
      var numFragments = ( byteRemaining / fragSize ) + 1;
      int i = 0;
      var responseCode = HttpStatusCode.OK;
      var jObject = new JObject();
  
      while (i &lt; numFragments)
      {
          var chunkSize = fragSize;
          var start = i * fragSize;
          var end = i * fragSize + chunkSize - 1;
          var offset = i * fragSize;
  
          if (byteRemaining &lt; chunkSize) {
              chunkSize = byteRemaining;
              end = fileSize - 1;
          }
  
          var contentRange = &quot; bytes &quot; + start + &quot;-&quot; + end + &quot;/&quot; + fileSize;
  
          using (var client = new HttpClient())
          {
              var content = new ByteArrayContent(file);
              content.Headers.Add(&quot;Content-Length&quot;, chunkSize.ToString());
              content.Headers.Add(&quot;Content-Range&quot;, contentRange);
  
              var response = client.PutAsync(url, content);
              var strData = response.Result.Content.ReadAsStringAsync().Result;
              responseCode = response.Result.StatusCode;
  
              
              if (responseCode == HttpStatusCode.Created)
              {
                  JObject data = JObject.Parse(strData);
                  string downloadUrl = data[&quot;@content.downloadUrl&quot;].ToString();
                  string itemId = data[&quot;id&quot;].ToString();
  
                  
                  fileSize = fileSize / 1000;
                  jObject = JObject.FromObject(new { name = fileName, id = itemId, url = downloadUrl, size = (double)fileSize });
              }
              
              else if (responseCode == HttpStatusCode.Conflict)
              {
                  var restart = RestartByteFile(upn, url, fileName);
                  responseCode = restart;
              }
          }
          byteRemaining = byteRemaining - chunkSize;
          i++;
      }
  
      if (responseCode == HttpStatusCode.Created) { return jObject; }
      else return jObject = JObject.FromObject(new { result = &quot;Fail&quot; });
  }
  </code></pre>