0

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?

dB'
  • 7,838
  • 15
  • 58
  • 101
  • That is probably just the print not showing you the content. See the netwerk tab of your browser and if you have an entry for file with filename in the request then the files are being sent to the server. – danial Feb 22 '16 at 16:36

1 Answers1

0

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
    })
Community
  • 1
  • 1
dB'
  • 7,838
  • 15
  • 58
  • 101