title: Save base64 file received from HttpPostedFileBase
body: <p>I am using jquery filedrop and I was getting an error using <code>readAsBinaryString</code> on Internet Explorer (10+). It works on Chrome and Firefox.</p>
  
  <p>Then I changed that method to <code>readAsDataURL</code>, which 'works' in every browser.</p>
  
  <p>The problem is that this last method returns an base64 file from <code>HttpPostedFileBase</code>and I don't know how to save the file as it is.</p>
  
  <p>Thats my code now:</p>
  
  <pre><code>byte[] binaryData;
  binaryData = new Byte[file.InputStream.Length];
  long bytesRead = file.InputStream.Read(binaryData, 0, (int)file.InputStream.Length);
  file.InputStream.Close();
  string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
  
  byte[] bytes = System.Convert.FromBase64String(base64String);
  
  string filePath = Path.Combine(ed_fisico_provisorio.vr_parametro, no_arquivo_provisorio);
  
  System.IO.FileStream fs = System.IO.File.Create(filePath);
  fs.Write(bytes, 0, bytes.Length);
  fs.Close();
  </code></pre>
  
  <p>But saving like this, the content of the file becomes like </p>
  
  <blockquote>
    <p>data:application/msword;base64,0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7...</p>
  </blockquote>
  
  <p>Any other ideas?</p>
  
  <p><strong>----- UPDATE -----</strong></p>
  
  <p>To clarify, step by step what is happening. By the way, I am using C# MVC.</p>
  
  <p><strong>STEP 1</strong> - Using the plugin jquery.filedrop.js, I drop the file on its view. Can be any file extension.</p>
  
  <p><strong>STEP 2</strong> - Calls the plugin, which includes this code:</p>
  
  <pre><code>// Respond to an upload
          function upload() {
  
              stop_loop = false;
  
              if (!files) {
                  opts.error(errors[0]);
                  return false;
              }
  
              var filesDone = 0,
                  filesRejected = 0;
  
              if (files_count &gt; opts.maxfiles &amp;&amp; opts.queuefiles === 0) {
                  opts.error(errors[1]);
                  return false;
              }
  
              // Define queues to manage upload process
              var workQueue = [];
              var processingQueue = [];
              var doneQueue = [];
  
              // Add everything to the workQueue
              for (var i = 0; i &lt; files_count; i++) {
                  workQueue.push(i);
              }
  
              // Helper function to enable pause of processing to wait
              // for in process queue to complete
              var pause = function (timeout) {
                  setTimeout(process, timeout);
                  return;
              }
  
              // Process an upload, recursive
              var process = function () {
  
                  var fileIndex;
  
                  if (stop_loop) return false;
  
                  // Check to see if are in queue mode
                  if (opts.queuefiles &gt; 0 &amp;&amp; processingQueue.length &gt;= opts.queuefiles) {
  
                      return pause(opts.queuewait);
  
                  } else {
  
                      // Take first thing off work queue
                      fileIndex = workQueue[0];
                      workQueue.splice(0, 1);
  
                      // Add to processing queue
                      processingQueue.push(fileIndex);
  
                  }
  
                  try {
                      if (beforeEach(files[fileIndex]) != false) {
                          if (fileIndex === files_count) return;
                          var reader = new FileReader(),
                              max_file_size = 1048576 * opts.maxfilesize;
  
                          reader.index = fileIndex;
                          if (files[fileIndex].size &gt; max_file_size) {
                              opts.error(errors[2], files[fileIndex], fileIndex);
                              // Remove from queue
                              processingQueue.forEach(function (value, key) {
                                  if (value === fileIndex) processingQueue.splice(key, 1);
                              });
                              filesRejected++;
                              return true;
                          }
                          reader.onloadend = send;
  
  
  
  
  
                          //reader.readAsArrayBuffer(files[fileIndex]);
  
  
                          reader.readAsDataURL(files[fileIndex]);
  
  
  
                          //reader.readAsBinaryString(files[fileIndex]);
  
                      } else {
                          filesRejected++;
                      }
                  } catch (err) {
                      // Remove from queue
                      processingQueue.forEach(function (value, key) {
                          if (value === fileIndex) processingQueue.splice(key, 1);
                      });
                      opts.error(errors[0]);
                      return false;
                  }
  
                  // If we still have work to do,
                  if (workQueue.length &gt; 0) {
                      process();
                  }
  
              };
  
              var send = function (e) {
  
                  var fileIndex = ((typeof (e.srcElement) === "undefined") ? e.target : e.srcElement).index
  
                  // Sometimes the index is not attached to the
                  // event object. Find it by size. Hack for sure.
                  if (e.target.index == undefined) {
                      e.target.index = getIndexBySize(e.total);
                  }
  
                  var xhr = new XMLHttpRequest(),
                      upload = xhr.upload,
                      file = files[e.target.index],
                      index = e.target.index,
                      start_time = new Date().getTime(),
                      boundary = '------multipartformboundary' + (new Date).getTime(),
                      builder;
  
                  newName = rename(file.name);
                  mime = file.type
                  if (typeof newName === "string") {
                      builder = getBuilder(newName, e.target.result, mime, boundary);
                  } else {
                      builder = getBuilder(file.name, e.target.result, mime, boundary);
                  }
  
                  upload.index = index;
                  upload.file = file;
                  upload.downloadStartTime = start_time;
                  upload.currentStart = start_time;
                  upload.currentProgress = 0;
                  upload.startData = 0;
                  upload.addEventListener("progress", progress, false);
  
  
                  xhr.open("POST", opts.url, true);
                  xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
  
                  // Add headers
                  $.each(opts.headers, function (k, v) {
                      xhr.setRequestHeader(k, v);
                  });
  
                  xhr.sendAsBinary(builder);
  
                  opts.uploadStarted(index, file, files_count);
  
                  xhr.onload = function () {
                      if (xhr.responseText) {
                          var now = new Date().getTime(),
                              timeDiff = now - start_time,
                              result = opts.uploadFinished(index, file, jQuery.parseJSON(xhr.responseText), timeDiff, xhr);
                          filesDone++;
  
                          // Remove from processing queue
                          processingQueue.forEach(function (value, key) {
                              if (value === fileIndex) processingQueue.splice(key, 1);
                          });
  
                          // Add to donequeue
                          doneQueue.push(fileIndex);
  
                          if (filesDone == files_count - filesRejected) {
                              afterAll();
                          }
                          if (result === false) stop_loop = true;
                      }
                  };
  
              }
  
              // Initiate the processing loop
              process();
  
          }
  </code></pre>
  
  <p><strong>STEP 3</strong> - The plugin return to the controller where I receive the file and try to save it:</p>
  
  <pre><code>[HttpPost]
  public ActionResult UploadFiles(IEnumerable&lt;HttpPostedFileBase&gt; files)
              {                
                      byte[] binaryData;
                      binaryData = new Byte[file.InputStream.Length];
                      long bytesRead = file.InputStream.Read(binaryData, 0, (int)file.InputStream.Length);
                      file.InputStream.Close();
                      string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
  
                      byte[] bytes = System.Convert.FromBase64String(base64String);
  
                      //Salva arquivo físico em pasta provisória
                      string filePath = Path.Combine(ed_fisico_provisorio.vr_parametro, file.FileName);
  
                      System.IO.FileStream fs = System.IO.File.Create(filePath);
  
                      fs.Write(bytes, 0, bytes.Length);
                      fs.Close();
      }
  </code></pre>
  
  <p>The content of the saved file is as said above.</p>
  
  <p>Any help will be appreciated.</p>