2015-07-20 71 views
0

我似乎無法弄清楚爲什麼一個字段失敗,而另一個字段卻無法成功上傳文件。我可以很好地上傳照片......但是當涉及到PDF時,我無法通過驗證。我已經爲我的代碼搜索了任何錯誤,但找不到任何東西。有趣的是,我可以通過手動上傳設置上傳照片,這對我來說沒有任何意義。我猜測我已經配置了flask-wtf和flask-uploads,但是由於在線上沒有關於如何使用這兩者的文檔,我只好盡力而爲。上傳Flask-Uploads和Flask-WTF失敗驗證

任何幫助將不勝感激,因爲我需要上傳pdfs,這讓我難住。

代碼如下...謝謝!

config.py

UPLOADED_MANUALS_DEST = "C:/pyprojects/cc/uploads/eq_manuals/" 
UPLOADED_MANUALS_ALLOW = 'pdf' 
UPLOADED_PHOTOS_DEST = "C:/pyprojects/cc/uploads/eq_photos/" 
UPLOADED_PHOTOS_ALLOW = 'png, jpg, jpeg' 

__init__.py

from flaskext.uploads import UploadSet, configure_uploads, patch_request_class 
photos = UploadSet('photos') 
manuals = UploadSet('manuals') 
configure_uploads(app, (photos, manuals)) 
patch_request_class(app) 

forms.py

class EQForm(Form): 
    name = StringField('Name', validators=[Required('Equipment Name is required.')]) 
    photo = FileField('Equipment Photo', validators=[ 
     FileAllowed(photos, 'Photo must be a png, jpg, or jpeg!')]) 
    manual = FileField('Equipment Manual', validators=[ 
     FileAllowed(manuals, 'Manual must be a pdf!')]) 

views.py

@app.route('/admin/equipment/', methods=['GET', 'POST']) 
@roles_accepted('admin') 
def equipment(): 

    eqform = EQForm() 

    if eqform.validate_on_submit(): 
     # os.path.join() the secured filename and the root path from config.py imported as join() 
     new_eq = Equipment(name = eqform.name.data) 

     if eqform.photo.data: 
      photo_path = join(
       app.config['UPLOADED_PHOTOS_DEST'], 
       secure_filename(eqform.photo.data.filename) 
       ) 

      eqform.photo.data.save(photo_path) 
      new_eq.photo_path = photo_path 

     if eqform.manual.data: 
      manual_path = join(
       app.config['UPLOADED_MANUALS_DEST'], 
       secure_filename(eqform.manual.data.filename) 
       ) 

      eqform.manual.data.save(manual_path) 
      new_eq.manual_path = manual_path 

     db.session.add(new_eq) 
     db.session.commit() 

    equipment = Equipment.query.all() 

    return render_template('equipment.html', 
     title="Equipment Database", 
     equipment = equipment, 
     eqform = eqform 
     ) 

equipment.html

 <form action="" method="POST" enctype="multipart/form-data" class="eq-field hide" name="eqform"> 
     {{ eqform.hidden_tag() }} 
      <ul class="list-group"> 
      <li class="list-group-item"> 
       {{ eqform.name.label }}: {{ eqform.name }} 
      </li> 
      <li class="list-group-item"> 
       {{ eqform.photo.label }}: {{ eqform.photo }} <br> 
       <span style="font-size:.75em;">Photo must be a png, jpg, or jpeg.</span> 
      </li> 
      <li class="list-group-item"> 
       {{ eqform.manual.label }}: {{ eqform.manual }} <br> 
       <span style="font-size:.75em;">Manual must be a pdf.</span> 
      </li> 
      </ul> 
      <input type="submit" class="btn btn-default" value="Submit"> 
     </form> 
+0

我一直在玩shell中的上傳集。看來我的問題與我的應用程序不正確使用我的配置中設置的項目有關。我的上傳集似乎都有默認的允許擴展名列表,而不是我定義的。 – Chockomonkey

+0

想通了......總是那麼愚蠢。我有我的擴展名列表作爲字符串。將'png,jpg,jpeg'改爲('png','jpg','jpeg')和VOILA!有趣的是......有一個擴展名的列表在我的PDF文件中仍然不起作用...所以我必須把它做成('pdf','PDF') – Chockomonkey

回答

0

是一個愚蠢的愚蠢的錯誤由我:

UPLOADED_MANUALS_ALLOW = 'pdf' 
UPLOADED_PHOTOS_ALLOW = 'png, jpg, jpeg' 

這兩個我已經聲明爲字符串是不允許的擴展列表。 ..更改爲:

UPLOADED_MANUALS_ALLOW = ('pdf') 
UPLOADED_PHOTOS_ALLOW = ('png', 'jpg', 'jpeg') 

然而,值得注意的:名單仍然沒有一個單一的項目工作,所以我不得不添加二:

UPLOADED_MANUALS_ALLOW =( 'PDF', 'PDF')