Search code examples
javascriptruby-on-railsangularjsng-file-upload

File uploads not working in rails


I'm trying to upload files to rails using ng-file-upload. My javascript looks like this...

$upload.upload({
  url: 'api/my_resource.json',
  data: {
    files: files
  }
})
console.log("files to upload", files);

... where files is an array of Files. The console prints

files to upload [File, File]

When I print the request parameters received by my Rails controller, though, I see this:

Parameters: {"data"=>"{\"files\":[{},{}]}"}

I seems that the files aren't being transmitted. Does anyone know what I'm doing wrong?


Solution

  • I couldn't get this to work using ng-file-upload. Instead I did it in straight javascript, as per here (single file uploads) and here (multiple files).

        fd = new FormData()
        for f in files
          fd.append("files[]", f);
    
        $http.post('/api/my_resource.json', fd, {
            withCredentials: true,
            headers: {'Content-Type': undefined },
            transformRequest: angular.identity
        })