2013-04-12 38 views
7

我想打一個多文件上傳form.I使用jQuery File Uploader。我的服務器端代碼:瓶:如何處理應用程序/八位字節流

@app.route("/new/photogallery",methods=["POST"]) 
def newPhotoGallery(): 
    print request.files 

我想兩件事情:

  1. 提交表單正常:

    當我正常提交我的形式,它打印:

    ImmutableMultiDict([('post_photo_gallery', FileStorage: u'' ('application/octet-stream'))])

  2. 使用AJAX提交表格:

    當我使用AJAX提交我的形式,它打印:

    ImmutableMultiDict([])

我的第一個問題是:爲什麼AJAX請求和正常的要求之間的差異。
我的第二個問題是:我該如何處理在Flask/Python 我的第三個問題是application/octet-stream要求:這是使用application/octet-stream的好辦法?

通過我不知道很多關於application/octet-stream。謝謝很多的方式。

+0

如果您使用Chrome,看看開發人員工具的網絡標籤,看看這兩個請求是什麼樣的。 – Blender

+0

------ WebKitFormBoundaryxInpKfVFK8mUuqOv Content-Disposition:form-data; NAME = 「post_photo_gallery」;文件名=「」 內容類型:應用程序/八位字節流線不出現在AJAX請求 – saidozcan

+0

這可能是有用的:https://github.com/blueimp/jQuery-File-Upload/wiki/Flask – Blender

回答

2

我無法獲得使用application/octet-stream類型帖子的請求,但在過去使用multipart/form-data類型表單使用瓶子上傳圖片。

我已經擴展了我在過去所做的那樣,以支持多個上傳文件,這已利用工作WERKZEUG的FileStorage對象。

這裏的關鍵是設置一個基於帖子的路徑,它正在尋找表單中的請求元素。這應該允許您通過標準表單或AJAX調用POST到路由。

下面是使用窗體一個簡單的例子:

模板的觀點:

<!DOCTYPE HTML> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>jQuery File Upload Example</title> 
</head> 
<body> 
{% if err %} 
    <h4>{{ err }}</h4> 
{% endif %} 

<form action="/" method=POST enctype=multipart/form-data id="fileupload"> 
    <input type="file" name="files" data-url="/" multiple> 
    <input type=submit value=Post> 
</form> 

{% if files %} 
{% for file in files %} 
<p>Uploaded: <b>{{ file }}</b> </p> 
{% endfor %} 
{% endif %} 
</body> 
</html> 

瓶應用

from flask import Flask, request, render_template 
from werkzeug import secure_filename, FileStorage 
import os 

# Flask functions 
app = Flask(__name__) 
app.config.from_object(__name__) 
DEBUG = True 
# add this so that flask doesn't swallow error messages 
app.config['PROPAGATE_EXCEPTIONS'] = True 

@app.route('/', methods=['GET', 'POST']) 
def uploader(): 
    if request.method =='POST' and request.files.getlist('files'): 
     up_file_list = [] 

     # Iterate the through a list of files from the form input field 
     for a_file in request.files.getlist('files'): 
      if a_file.filename: 
       # Validate that what we have been supplied with is infact a file 
       if not isinstance(a_file, FileStorage): 
        raise TypeError("storage must be a werkzeug.FileStorage") 
       # Sanitise the filename 
       a_file_name = secure_filename(a_file.filename) 
       # Build target 
       a_file_target = os.path.join('/tmp/', a_file_name) 
       # Save file 
       a_file.save(a_file_target) 
       up_file_list.append(a_file_name) 
     # Return template 
     if up_file_list: 
      return render_template('uploader.html', err=None, files=up_file_list) 
     else: 
      return render_template('uploader.html', err='No Files Uploaded', files=None) 
    else: 
     return render_template('uploader.html', err=None, files=None) 


# application execution 
if __name__ == '__main__': 
    app.run() 
相關問題