簡體   English   中英

文件上傳在Rails中不起作用

[英]File uploads not working in rails

我正在嘗試使用ng-file-upload將文件上傳到Rails。 我的JavaScript看起來像這樣...

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

...其中filesfiles數組。 控制台打印

files to upload [File, File]

但是,當我打印Rails控制器收到的請求參數時,會看到以下內容:

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

我似乎沒有傳輸文件。 有人知道我在做什么錯嗎?

我不能使用ng-file-upload來工作。 取而代之的是,我按照此處 (單個文件上傳)和此處 (多個文件)的簡單javascript進行操作。

    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
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM