2012-12-05 56 views
0

上傳圖像時,我試圖上傳圖片到回形針,並將其保存到S3編碼錯誤。不過,我得到以下錯誤在我的控制檯通過回形針

!! Unexpected error while processing request: invalid byte sequence in UTF-8

有關於如何解決此問題在計算器上一些反應,但多數指向原來的解決方案是到機架的更新。不過,我使用Ruby 1.9.3和Rails 3.1.3,相信我沒有機架(我還沒有安裝它作爲一個寶石,我??)。

,我一直在努力的文件名是相當簡單的,所以我假設的問題是實際的文件,但我不知道如何調試錯誤來自哪個上傳變量。 Rails是不把任何在日誌文件中的這些錯誤,所以我似乎無法獲得更多細節。

我的控制是相當簡單的,就像回形針GitHub的文檔

 
def create 
    wine_photo = WinePhoto.create(params[:wine_photo]) 

    return render :json => wine_photo 

    end 

雖然最初我所常用的

 
    wine_photo - WinePhoto.new(params[:wine_photo]) 
    if wine_photo.save 
    return render :json => wine_photo 
    else 
    return render :json => wine_photo.errors 
    end 

我的模型(我懷疑是非常有幫助)的例子是

 
class WinePhoto true 
    validates_with AttachmentPresenceValidator, :attributes => :photo 

    belongs_to :wine 
    belongs_to :user 

    def photo_url 
     photo.url 
    end 
end 

根據此回覆在stackoverflow上,Ruby Invalid Byte Sequence in UTF-8,我已經在我的控制器中嘗試了以下內容

 
def create 
    wine_photo = WinePhoto.new(params[:wine_photo]) 
    wine_photo.photo = IO.read(wine_photo.photo).force_encoding("ISO-8859-1").encode("utf-8", replace: nil) 
... 

但仍然出現錯誤。

如何突破這個編碼問題有什麼建議?有沒有一種方法,以確認從文件即將被上傳的錯誤?

我上傳的代碼(AJAX)是

 
save_photo: function(){ 
     var file = document.getElementById('file_api').files[0]; 
     console.log(file); 
    var xhr = new XMLHttpRequest(); 
     if (xhr.upload && file.type == "image/jpeg") { 

      // create progress bar 
      var o = document.getElementById("progress"); 
      var progress = o.appendChild(document.createElement("p")); 
      progress.appendChild(document.createTextNode("upload " + file.name)); 


      // progress bar 
      xhr.upload.addEventListener("progress", function(e) { 
       var pc = parseInt(100 - (e.loaded/e.total * 100)); 
       progress.style.backgroundPosition = pc + "% 0"; 
      }, false); 

      // file received/failed 
      xhr.onreadystatechange = function(e) { 
       if (xhr.readyState == 4) { 
        progress.className = (xhr.status == 200 ? "success" : "failure"); 
       } 
      }; 

      // start upload 
      xhr.open("POST", document.getElementById("add_photo").action, true); 
      xhr.setRequestHeader("X_FILENAME", file.name); 
      xhr.send(file); 
      } 
     } 

file的PARAMS是

 
File {webkitRelativePath: "", lastModifiedDate: Thu Nov 10 2011 09:40:39 GMT+1100 (AUS Eastern Summer Time), name: "WP_000012.jpg", type: "image/jpeg", size: 1344450} 

回答