繁体   English   中英

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

[英]Rails 4.2 - Send Image Data as Email Attachment Without Saving Image

我的客户端应用程序是用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请求中发送HTTP Body不是一个好主意,因此我将方法更改为POST。 现在request.body.read正在返回我编码的String! 我还在请求中添加了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发送而不进行字符串编码,然后将其发送回来,这样就可以了! 这是我的最终代码:

迅速

// 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字符串编码。 这段代码很棒!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM