Rails 4.2-将图像数据作为电子邮件附件发送,而不保存图像



我的客户端应用程序是用Swift编写的iOS应用程序。在那个iOS应用程序中,我将图像转换为Base64编码的字符串,然后在HTTP请求的主体中将该字符串发送到我的Rails服务器。这是我这样做的代码:

// Create a new URL request with the URL
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
// Set the HTTP method to POST
request.HTTPMethod = "POST"
// Set the content type of the request to XML
request.setValue("application/xml", forHTTPHeaderField: "Content-Type")
// Convert the image to binary and then to a Base 64 Encoded String
let imageData:String = UIImagePNGRepresentation(image).base64EncodedStringWithOptions(nil)
// Set the HTTP Body of the request to the image
request.HTTPBody = NSData(base64EncodedString: imageData, options: nil)

在处理请求的Rails控制器中,我想检索图像并将其作为电子邮件附件发送。我不想把图像保存在任何地方;我只想解码内存中的图像,然后以某种方式将其用作电子邮件附件。

如何在Rails控制器中检索图像数据,并以允许我将其作为电子邮件附件发送回的方式对其进行解码?

为了检索图像数据,我尝试使用request.body.read,但由于某种原因,这会返回一个空字符串。

提前感谢您的帮助!

编辑:

request.body.read返回了一个空字符串,因为我使用了GET请求。从那以后,我了解到在GET请求中发送HTTPBody不是一个好主意,所以我将方法改为POST。现在request.body.read正在返回我的编码字符串!我还在请求中添加了Content-Type标头。

尽管如此,我还是不知道如何正确地解码HTTP Body并将其分配给某种图像对象

编辑#2:

我已经使用以下代码在我的邮件中发送了电子邮件附件:

attachments["file.png"] =
{
   mime_type: 'image/png',
   content: Base64.decode64(request.body.read)
}

不幸的是,当我在电子邮件中收到PNG文件时,它无法打开。我不知道编码是否能很好地从Swift转换为Ruby。我会继续调查的。

编辑#3:

我删除了Base64字符串编码,它工作得很好!请参阅下面我发布的答案。

我想通了!我认为Base64编码/解码在Swift和Ruby上的执行方式不同,所以我决定将图像作为NSData发送,而不使用String编码,然后将其发送回,结果成功了!这是我的最终代码:

Swift

// Create a URL
let url:NSURL = NSURL(string: urlString)!
// Create a new URL request with the URL
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
// Set the HTTP method to POST
// GET requests usually do not have an HTTP Body, and it's considered a very bad idea to include one in a GET request
request.HTTPMethod = "POST"
// Set the content type of the request to XML
request.setValue("application/xml", forHTTPHeaderField: "Content-Type")
// Convert the image to binary data
let imageData:NSData = UIImagePNGRepresentation(image)
// Set the HTTP Body of the request to the image
request.HTTPBody = imageData
// Create a new queue
let queue:NSOperationQueue = NSOperationQueue()
// Send the async request
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:
{ (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in
    println(response)
})

您可以将此代码放在任何方法中。

Ruby on Rails(Ruby 2.1.1和Rails 4.2.0)

class ImageApiController < ApplicationController
  # Skip verifying the authenticity token since a form wasn't submitted; a web request was sent
  skip_before_filter :verify_authenticity_token, :only => [ :index ]
  def index
    params[:image] = request.body.read
    # Send the email
    UserMailer.image_attachment_email(params).deliver_now
    respond_to do |format|
      format.html
      format.js  { render plain: "Test" }
      format.xml { render plain: "Test" }
    end
  end
end

然后在UserMailer:

class UserMailer < ApplicationMailer
  def image_attachment_email(params)
    attachments["image.png"] =
    {
      mime_type: 'image/png',
      content: params[:image]
    }
    # Send an email
    mail(to: "user@example.com", subject: "Image")
  end
end

所以实际上不需要Base64字符串编码。这个代码工作得很好!

最新更新