我正在实施和mvc rails app, 我可以将pdf附加到电子邮件并发送 。 但是当我试图从我的Gmail中打开附件时,浏览器会显示:
无法加载PDF文档
控制器:
def update
if !params[:attachment].nil? && !params[:attachment].blank?
@file = Tempfile.new([params[:attachment], '.pdf'])
s = StringIO.new(params[:attachment])
def s.original_filename; "Answer.pdf"; end
@file_name = s.original_filename
else
@file = ""
@file_name = ""
end
respond_to do |format|
if @study_request.update(study_request_params)
format.html { redirect_to study_requests_path, notice: 'Answer Sended.' }
format.json { render :show, status: :ok, location: @study_request }
enviar()
else
format.html { render :edit }
format.json { render json: @study_request.errors, status: :unprocessable_entity }
end
end
end
def enviar()
mail = StudyRequestMailer.new_analysis(@study_request,@file_name,@file)
mail.deliver_now
end
查看:
<div class="ui form">
<%= form_with(model: study_request, local: true) do |form| %>
.
.
.
.
.
<div class="field">
<%= file_field_tag :attachment,:class=>"form-control btn-metis-7 btn-file" %>
<%= form.submit "Send Mail", :class => 'ui fluid large teal submit button' %>
</div>
<%end%>
</div>
邮件程序:
class StudyRequestMailer < ApplicationMailer
def new_analysis(studyRequest,file_name,file)
@studyRequest = studyRequest
if file != ""
attachments[file_name] = File.read(file)
end
mail to: @studyRequest.email, subject: "Answer"
end
end