2011-09-16 6 views
0

양식에서 이메일을 보내려고하면 오류 메시지가 표시됩니다. POST 요청으로 파일을 어떻게 처리해야합니까? 나는 blobstore에 그것을 쓸 필요가 없다. 이메일로 보내면된다.GAE가있는 양식에서 첨부 파일을 보내려면 어떻게합니까?

템플릿 :

<form method="POST" action="{{form_url}}" name="upload" enctype="multipart/form-data"> 
<table border="0"><tr><td colspan="2">  
<div class="labelform"> 
    </div> 
    <div><input type="hidden" id="lng" name="lng" size="35" maxlength="50" value="" /></div> 
<div class="labelform">  
    </div> 
    <div><input type="hidden" id="lat" name="lat" size="35" maxlength="50" value="" /> 
<input type="hidden" id="place" name="place" size="55" maxlength="55" value="" /> 
</div>   
    </td><td rowspan="9">  
    </td></tr>  
{% for field in form %}  
<tr><td> 
     <div class="labelform" style="display: block;"> 
     <label>{% trans "Type of problem" %}:</label> 
     </div> 
     </td><td> 
</label> <select name="subject" id="subject"> 
      <option value="{% trans "Problems with ads" %}" >{% trans "Problems with ads" %}</option> 
      <option value="{% trans "Advertising" %}" >{% trans "Advertising" %}</option> 
      <option value="{% trans "Images" %}" >{% trans "Images" %}</option> 
      <option value="{% trans "Our rules when advertising" %}" >{% trans "Our rules when advertising" %}</option> 
      <option value="{% trans "Technical problems" %}" >{% trans "Technical problems" %}</option> 
      <option value="{% trans "Other" %}" >{% trans "Other" %}</option>   
      </select> 
       </div> 
    </td></tr> 
<tr><td> 
    <div class="fieldWrapper"> 
     {{ form.name.errors }} 
     <label for="id_subject">{% filter capfirst %}{% trans "name" %}{% endfilter %}</label></td><td> 
     <div> 
    <input type="text" id="name" name="name" value="{{ user.nickname }}{% if not user %}{{ current_user.name|escape }}{% endif %}{% if not user and not current_user %}{% ifequal twittername None %}{% else %}{{ twittername }}{% endifequal %}{% endif %}" size="35" maxlength="50" /> 
    <div id="err_name" style="display: none;"> 
     <span class="warning" id="err_msg_name"></span> 
    </div> 
    </div></td></tr> 
    </div><tr><td> 
    <div class="fieldWrapper"> 
     {{ form.email.errors }} 
     <label for="id_sender">{% trans "E-mail address" %}</label></td><td> 
     {{ form.email }}</td></tr> 

    </div><tr><td valign="top"> 
    <div class="fieldWrapper"> 
     {{ form.text.errors }} 
     <label for="id_message">{% trans "Text" %}</label></td><td> 
     {{ form.text }}</td></tr> 
    </div> 
    </div> 
    <tr><td> </td><td> <input type="file" name="file" /><br>{% trans "If there is a problem with the images - upload them here" %}<br/> 
<input type="submit" value="Submit" /> </td></tr></table> 

코드 : 당신은 당신이 같은 그것으로 작업을해야하므로 Blob 저장소에 쓰기의 대명사 가지고 GAE에서

class ContactFileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):#to do:attachment 
    def post(self): 
     message = mail.EmailMessage(sender='[email protected]', subject=self.request.POST.get('subject'))#, attachments=[('test', self.request.POST.get('file').file.read()) ]) 
     message.body = ('%s \n%s \n%s \nhttp://...com/') % (self.request.POST.get('name'), self.request.POST.get('email'), self.request.POST.get('text')) 
     message.to='[email protected]' 
     message.send() 
     self.redirect('/customer_service.htm') 
+0

"오류 메시지 가져 오기"는별로 도움이되지 않습니다. 스택 추적을 포함하십시오. –

답변

1

파일을 업로드.

from google.appengine.ext import blobstore 

upload_files = self.get_uploads('file') 
blob_info = upload_files[0] 
blob_reader = blobstore.BlobReader(blob_info.key()) 

message.attachments = [blob_info.filename,blob_reader.read()] 

언제든지 삭제할 수 있습니다.

blob = blobstore.BlobInfo.get(blob_info.key()) 
blob.delete() 
+0

그것은 작동합니다. 고마워요 –

+1

문제가되지 않습니다 :) –

+0

파일을 저장할 필요가 없다면 이메일 첨부 파일을 따라 파일이 자동으로 삭제되도록 다른 방법이 있습니까? 삭제는 즉각적 일 필요는 없으며, 명시 적으로 삭제할 필요가없는 방법을 찾아 내려고 노력하고 있습니다. 또한 HTML5 파일 업로드 (많은 jQuery 기반 선택을 구현해야 함)를 사용하거나 기존 파일 업로드를 사용해야합니까? –

관련 문제