title: Sharepoint UploadFile 'Specified argument was out of range of valid values'
body: <p>I'm trying to duplicate a file from one library to another within a Remote Event Receiver.</p>
  
  <p>When reaching the <code>UploadFile</code>-function, I get the following error:</p>
  
  <p><code>Specified argument was out of the range of valid values. Parameter name: bytesToCopy</code></p>
  
  <p>Information about the function can be found <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.client.filefolderextensions.uploadfile" rel="nofollow noreferrer">here</a>. It has the following signature:</p>
  
  <pre><code>public static Microsoft.SharePoint.Client.File UploadFile (this Microsoft.SharePoint.Client.Folder folder, string fileName, System.IO.Stream stream, bool overwriteIfExists);
  </code></pre>
  
  <p>My code is:</p>
  
  <pre><code>using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
  {
      ClientResult&lt;Stream&gt; data = curItem.File.OpenBinaryStream();
      clientContext.ExecuteQuery();
  
      if (data != null &amp;&amp; data.Value != null)
      {
  
           data.Value.CopyTo(stream);
           UploadedFile = destinationList.RootFolder.UploadFile(curItem.File.Name.ToString(), stream, true);
      }
  }
  </code></pre>
  
  <p>As a note: <code>curItem</code>, <code>destinationList</code> and <code>curItem.File</code> are all loaded in the context.</p>
  
  <p>Edit, complete error:</p>
  
  <pre><code>   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
     at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
     at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext clientContext, Int32 retryCount, Int32 delay, String userAgent)
     at Microsoft.SharePoint.Client.FileFolderExtensions.UploadFile(Folder folder, String fileName, Stream stream, Boolean overwriteIfExists)
     at PG.SHP.SCE.QCSPAddInWeb.Services.AppEventReceiver.HandleItemCheckedIn(SPRemoteEventProperties properties) in C:\Users\OBJ_JDWR\Source\Workspaces\SharePoint\SCE\Main\Source\PG.SHP.SCE\Source\PG.SHP.SCE.QCSPAddInWeb\Services\AppEventReceiver.svc.cs:line 288
  </code></pre>
  
  <p>Line 288:
  <a href="https://i.stack.imgur.com/F4WTy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F4WTy.png" alt="enter image description here"></a></p>
  
  <p>How to get rid of the error?</p>