title: File Upload in AngularJs - Error: XMLHttpRequest cannot load URL - status: -1
body: <p>I am trying to do a file upload option using AngularJs in Jive. I am trying the below code, but I am getting the error  as below:</p>
  
  <pre><code>OPTIONS https://test.com/MyApp/api/Seasons/UploadImage 405 (Method Not Allowed)
  
  XMLHttpRequest cannot load https://test.com/MyApp/api/Seasons/UploadImage. Response for preflight has invalid HTTP status code 405
  
  status: -1
  StatusText: ""
  </code></pre>
  
  <p>Below is the code which i tried:</p>
  
  <p>//Index.html</p>
  
  <pre><code>&lt;input type="file" class="form-control" id="imageUploadfile" my-files="files" name="Imagefile" accept="image/*" /&gt;
  &lt;input type="button" name="imageUploadButton" ng-click="uploadFiles()" value="Upload" /&gt;
  </code></pre>
  
  <p>//controller.js</p>
  
  <pre><code>$scope.uploadFiles = function () {
  
          var config = {
              headers: {
                  "Content-Type": undefined,
              }
          };
  
         var file = sharedDataService.shared.files[0];
  
      var url = _testBaseUrl + 'api/Seasons/UploadImage';
  
  
     $http.post(url, file, config).
  
           then(function (response) {
               $scope.result = "SUCCESS";
  
             $scope.data = response.data.data;
           }).catch(function (response) {
               alert( "ERROR " + response.status);
           });
      };
  </code></pre>
  
  <p>I do not have an endpoint added in web.config. Below is the handler config in web.config</p>
  
  <pre><code> &lt;handlers accessPolicy="Read, Script"&gt;
        &lt;remove name="WebDAV" /&gt;
        &lt;remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /&gt;
        &lt;add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit"
           path="*."
           verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
           modules="IsapiModule"
           scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
           preCondition="classicMode,runtimeVersionv4.0,bitness64"
           responseBufferLimit="0" /&gt;
        &lt;remove name="ExtensionlessUrlHandler-Integrated-4.0" /&gt;
        &lt;remove name="OPTIONSVerbHandler" /&gt;
        &lt;remove name="TRACEVerbHandler" /&gt;
        &lt;add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /&gt;
      &lt;/handlers&gt;
  </code></pre>
  
  <p>In the web api method:</p>
  
  <pre><code>[HttpPost]
          [Route("Seasons/UploadImage")]
          public async Task&lt;HttpResponseMessage&gt; UploadImage()
          {
              Dictionary&lt;string, object&gt; dict = new Dictionary&lt;string, object&gt;();
              try
              {
  
                  var httpRequest = HttpContext.Current.Request;
  
                  foreach (string file in httpRequest.Files)
                  {
                      HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
  
                      var postedFile = httpRequest.Files[file];
                      if (postedFile != null &amp;&amp; postedFile.ContentLength &gt; 0)
                      {
  
                          int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB  
  
                          IList&lt;string&gt; AllowedFileExtensions = new List&lt;string&gt; { ".jpg", ".gif", ".png", "jpeg" };
                          var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                          var filenameOrigin = postedFile.FileName.Substring(0, postedFile.FileName.LastIndexOf('.'));
                          var extension = ext.ToLower();
                          if (!AllowedFileExtensions.Contains(extension))
                          {
                              var message = string.Format("Please Upload image of type .jpg,.gif,.png,.jpeg.");
  
                              dict.Add("error", message);
                              return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
                          }
                          else if (postedFile.ContentLength &gt; MaxContentLength)
                          {
                              var message = string.Format("Please Upload a file upto 2 mb.");
  
                              dict.Add("error", message);
                              return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
                          }
                          else
                          {
  
                              string filename = String.Format(@"{0}_{1}_{2}{3}", filenameOrigin, Guid.NewGuid(), DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss"), extension);
                              var filePath = HostingEnvironment.MapPath("~/Images/" + filename);
  
                              postedFile.SaveAs(filePath);
                              if (!String.IsNullOrEmpty(filePath))
                              {
                                  dict.Add("filePath", filePath);
                              }
                          }
                      }
  
                      var messageSuccess = string.Format("Image Updated Successfully.");
                      dict.Add("Success", messageSuccess);
                      return Request.CreateResponse(HttpStatusCode.Created, dict);
                  }
                  var res = string.Format("Please Upload a image.");
                  dict.Add("error", res);
                  return Request.CreateResponse(HttpStatusCode.NotFound, dict);
              }
              catch (Exception ex)
              {
                  var res = string.Format("something went wrong");
                  dict.Add("error", res);
                  return Request.CreateResponse(HttpStatusCode.NotFound, dict);
              }
          }
  </code></pre>
  
  <p>This is a cross-domain call. How to fix this?
  Thanks</p>