title: Angularjs, File Upload and IE9
body: <p><strong>Having file upload problems in an Angularjs/Nodejs app.</strong> </p>
  
  <p>I implemented this <a href="https://github.com/danialfarid/angular-file-upload" rel="nofollow">angular-file-upload</a> directive. Using it, file upload works in all modern browsers but it fails in IE9. By "fails" I mean that it acts like it's working... I can log the file upload progress and see it progressively reach 100%, a success log message is triggered in the success handler (nothing is triggered in the error handler) but the file never appears in the upload directory (on the same machine).</p>
  
  <p>There are no console errors of any sort and I set the upload directory permissions to 777 in order to rule out that.</p>
  
  <p><strong>The things that I have found that are off...</strong> first, files uploaded in IE9 never trigger log messages I have placed in my uploadPhoto function so the route appears to not even be hitting (thought files uploaded in modern browsers DO trigger these log messages). Also, after an upload, in the Network panel of the developer tools the value for the Type should not be text/html as it is below. </p>
  
  <p>(the below maps to: <code>URL, Method, Result, Type, Received</code>): </p>
  
  <pre><code>/path/to/upload-dir/filename.jpg GET 200 text/html 20.7kb
  </code></pre>
  
  <p>Here is my file upload handler:</p>
  
  <pre><code>$scope.onFileSelect = function ($file) {
      $scope.ajaxVisible = true;
      $scope.uploadError = false;
      var photo = $file[0];
      $scope.upload = $upload.upload({
          url: '/upload/photo',
          file: photo,
          method: 'POST'
      }).progress(function (evt) {
          }).success(function (data, status, headers, config) {
              $scope.toggleUploadForm();
              $scope.imgsrc = 'path/to/upload/dir/' + photo.name;
              User.currentUser.profile_image = photo.name;
              User.updateUser();
              $scope.ajaxVisible = false;
          }).error(function (err) {
              $scope.uploadError = true;
              $scope.ajaxVisible = false;
          });
  };
  </code></pre>
  
  <p>Here is the handler for that route (expressjs/node):</p>
  
  <pre><code>app.post('/upload/photo', userAPI.uploadPhoto);
  </code></pre>
  
  <p>Here is uploadPhoto that is referenced in the route handler:</p>
  
  <pre><code>exports.uploadPhoto = function(req, res) {
      logger.info('inside uploadPhoto'); // &lt;-- never reached using IE9
      var callbacks = {};
  
      callbacks.uploadSuccess = function(){
          res.json('success');
      };
  
      callbacks.uploadFailure = function(err){
          res.json(400, 'Error uploading image.');
      };
  
      user.handlePhotoUpload(req.files, callbacks);
  };
  </code></pre>
  
  <p>Which, at long last, hands things off to:</p>
  
  <pre><code>this.handlePhotoUpload = function(params, callbacks) {
          logger.log('inside handlePhotoUpload'); // &lt;-- never reached using IE9
          if(params.file.mime !== 'image/png' &amp;&amp; params.file.mime !== 'image/jpeg' &amp;&amp; params.file.mime !== 'image/gif') {
              callbacks.uploadFailure('Wrong file type');
              return;
          }
          fs.readFile(params.file.path, function(err, data){
              if(err){
                  callbacks.uploadFailure(err);
              }
              var newPath = path.resolve("./path/to/upload/dir/" + params.file.filename);
              fs.writeFile(newPath, data, function(err) {
                  if(err){
                      callbacks.uploadFailure(err);
                  }
  
                  callbacks.uploadSuccess();
              });
          });
      };
  </code></pre>
  
  <p>This directive utilizes the <a href="https://github.com/mailru/FileAPI" rel="nofollow">FileAPI library</a> on the backend and requires Flash to handle the uploading process. I assume that Flash is the source of the problem. I'm missing something with it but not sure what it might be. </p>
  
  <p>As I said, no errors in console (in any browser) and the file upload shows all the signs of working except that the file never appears in the upload dir. But, various log statements which are reached when uploading via a modern browser are never reached when uploading via IE9. </p>
  
  <p>Suggestions as to how to troubleshoot this are as welcome as suggestions of solutions. </p>