从 iOS 客户端插入 MongoDB 时 BSON::InvalidDocument

发布于 2024-11-05 12:14:49 字数 1804 浏览 1 评论 0原文

我正在创建一个带有 Rails/mongoid 后端的基本博客应用程序和一个具有带有名称和图像的帖子模型的 iOS 客户端。当我从 iOS 应用程序创建新的 pos 时,出现以下错误。

  BSON::InvalidDocument 
(Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON.):
      app/controllers/posts_controller.rb:47:in `create'
      app/controllers/posts_controller.rb:46:in `create'

在 iOS 端,我使用 ASIHttpRequest 和以下代码:

 NSURL *url=[[NSURL alloc] initWithString:@"http://localhost:3000/posts"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"Ben" forKey:@"[post] name"];
    [request setFile:@"/Users/Seb/Desktop/beach.jpeg" forKey:@"[post] image"]; 
    [request startSynchronous];  

服务器日志显示:

Started POST "/posts" for 127.0.0.1 at Thu May 05 14:37:10 -0700 2011
  Processing by PostsController#create as HTML
  Parameters: {"post"=>{" name"=>"Ben", " image"=>#<ActionDispatch::Http::UploadedFile:0x103e48fc8 @content_type="image/jpeg", @original_filename="beach.jpeg", @headers="Content-Disposition: form-data; name=\"[post] image\"; filename=\"beach.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/PI/PI+kcHrCHfuDh-K+ppxAxE+++TI/-Tmp-/RackMultipart20110505-11613-x75qie-0>>}}
Completed   in 21ms

BSON::InvalidDocument (Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON.):
  app/controllers/posts_controller.rb:47:in `create'
  app/controllers/posts_controller.rb:46:in `create'

Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.3ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (9.5ms)

I'm creating a basic blog app with rails/mongoid backend and an iOS client that has a posts model with name and image. I'm getting the following error when I create a new pos from an iOS app.

  BSON::InvalidDocument 
(Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON.):
      app/controllers/posts_controller.rb:47:in `create'
      app/controllers/posts_controller.rb:46:in `create'

On the iOS side I'm using ASIHttpRequest with the following code:

 NSURL *url=[[NSURL alloc] initWithString:@"http://localhost:3000/posts"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"Ben" forKey:@"[post] name"];
    [request setFile:@"/Users/Seb/Desktop/beach.jpeg" forKey:@"[post] image"]; 
    [request startSynchronous];  

The server log shows:

Started POST "/posts" for 127.0.0.1 at Thu May 05 14:37:10 -0700 2011
  Processing by PostsController#create as HTML
  Parameters: {"post"=>{" name"=>"Ben", " image"=>#<ActionDispatch::Http::UploadedFile:0x103e48fc8 @content_type="image/jpeg", @original_filename="beach.jpeg", @headers="Content-Disposition: form-data; name=\"[post] image\"; filename=\"beach.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/PI/PI+kcHrCHfuDh-K+ppxAxE+++TI/-Tmp-/RackMultipart20110505-11613-x75qie-0>>}}
Completed   in 21ms

BSON::InvalidDocument (Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON.):
  app/controllers/posts_controller.rb:47:in `create'
  app/controllers/posts_controller.rb:46:in `create'

Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.3ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (9.5ms)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聽兲甴掵 2024-11-12 12:14:49

错误消息表明以下类无法序列化为 BSON:ActionDispatch::Http::UploadedFile

根据您在服务器日志中的帖子,您正在发送一个带有不寻常的“图像”字段的 JSON 对象。我怀疑问题就出在这里。

您的文件似乎被存储到驱动器上的临时位置。您是否尝试保存文件的实际字节或仅保存此临时位置?你在这里使用 GridF 吗?

你能确认该类确实可以序列化为 BSON 吗?
序列化做的事情正确吗?

The error message indicates that the following class cannot be serialized into BSON: ActionDispatch::Http::UploadedFile.

Based on your post in the server logs, you're shipping up a JSON object with an unusual "image" field. I suspect the problem lies here.

It looks like your files is being stored to a temp location on the drive. Are you trying to save the actual bytes of the file or just this temp location? Are you using GridFs here?

Can you confirm that the class can indeed be serialized to BSON?
Is that serialization doing the right thing?

挖鼻大婶 2024-11-12 12:14:49

该错误非常明显,除非您想使用 paperclip 或其他解决方案,否则您将拥有自己做。

例如,如果您的模型中碰巧有一个字段 :blob 和字段 :filename ,那么为了保存,您需要在模型中包含如下内容:


class Post
...
  def image=(img)
    self.filename = img.original_filename
    self.blob = img.read
  end
end

The error is quite obvious, and unless you want to use paperclip or other solution, you'll have to do it yourself.

For example, if you happend to have a field :blob and field :filename in your model, then for saving you'll need to have something like this in your model:


class Post
...
  def image=(img)
    self.filename = img.original_filename
    self.blob = img.read
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文