上传图像时,我试图上传图片到回形针,并将其保存到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}