title: How to save any file in sql server with field type varbinary(max) from angular using asp .NET core for rest API?
body: <p>I am trying to save a file (for example an image) in the Microsoft SQL Server Management Studio using asp .NET core for the Rest API. I have successfully create a base64 of the file but I do not understand in which format I have to change the base64 in order to be able to put the file in the database that the field type is varbinary(max). In the asp .NET core the type of that field is byte[].</p>
  <p>So, I have tried to convert the base64 to blob but it had a validation error and then I tried to change base64 to binary but it had also a validation error using <a href="https://www.codegrepper.com/code-examples/javascript/convert+base64+to+binary+javascript" rel="nofollow noreferrer">this</a> suggested code. So, apparently this is not what I need.</p>
  <p>The code I used for base64 to blob is:</p>
  <pre><code>base64ToBlob(base64, mime) {
    mime = mime || '';
    var sliceSize = 1024;
    var byteChars = window.atob(base64);
    var byteArrays: any = [];
  
    for (
      var offset = 0, len = byteChars.length;
      offset &lt; len;
      offset += sliceSize
    ) {
      var slice = byteChars.slice(offset, offset + sliceSize);
  
      var byteNumbers = new Array(slice.length);
      for (var i = 0; i &lt; slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }
  
      var byteArray = new Uint8Array(byteNumbers);
  
      byteArrays.push(byteArray);
    }
  
    return new Blob(byteArrays, { type: mime });
  }
  </code></pre>
  <p>and the code that I used for base64 to binary is:</p>
  <pre><code>_base64ToArrayBuffer(base64) {
    var binary_string = window.atob(base64);
    var len = binary_string.length;
    var bytes = new Uint8Array(len);
    for (var i = 0; i &lt; len; i++) {
      bytes[i] = binary_string.charCodeAt(i);
    }
    return bytes.buffer;
  }
  </code></pre>
  <p>So what format should I change base64 to in order to save my file to the database?</p>