2013-04-12 32 views
7

tôi muốn tạo một tệp tải lên nhiều tệp.I sử dụng jQuery File Uploader. My server-side code:Bình: Cách xử lý ứng dụng/octet-stream

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

tôi đã cố gắng hai điều:

  1. Gửi hình thức bình thường:

    Khi tôi nộp mẫu đơn của tôi bình thường, nó in:

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

  2. Gửi biểu mẫu bằng AJAX:

    Khi tôi nộp mẫu đơn của tôi sử dụng AJAX, nó in:

    ImmutableMultiDict([])

câu hỏi đầu tiên của tôi là: Tại sao lại có một sự khác biệt giữa yêu cầu AJAX và yêu cầu bình thường.
Câu hỏi thứ hai của tôi là: Làm cách nào tôi có thể xử lý yêu cầu application/octet-stream này trong Flask/Python Câu hỏi thứ ba của tôi là: Đây có phải là cách tốt để sử dụng application/octet-stream không?

Bằng cách này tôi không biết nhiều về application/octet-stream. Cảm ơn bạn rất nhiều.

+0

Nếu bạn đang ở trên Chrome, hãy xem tab Mạng của công cụ nhà phát triển và xem hai yêu cầu trông như thế nào. – Blender

+0

------ WebKitFormBoundaryxInpKfVFK8mUuqOv Nội dung-Bố trí: biểu mẫu-dữ liệu; name = "post_photo_gallery"; filename = "" Loại nội dung: dòng ứng dụng/octet-stream không xuất hiện trên yêu cầu AJAX – saidozcan

+0

Điều này có thể hữu ích: https://github.com/blueimp/jQuery-File-Upload/wiki/Flask – Blender

Trả lời

2

Tôi không thể yêu cầu sử dụng application/octet-stream bài đăng loại nhưng đã sử dụng multipart/form-data các loại biểu mẫu trong quá khứ để tải hình ảnh lên bằng bình.

Tôi đã mở rộng những gì tôi đã làm trong quá khứ để hỗ trợ nhiều tệp tải lên và điều này đã làm việc tận dụng các đối tượng FileStorage của werkzeug.

Chìa khóa ở đây là thiết lập tuyến đường dựa trên bài đăng đang tìm kiếm phần tử yêu cầu từ biểu mẫu. Điều này sẽ cho phép bạn POST lên tuyến đường hoặc thông qua một hình thức tiêu chuẩn hoặc một cuộc gọi AJAX.

Dưới đây là một ví dụ đơn giản đó là sử dụng một hình thức:

Template cho quan điểm:

<!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> 

Flask App

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() 
Các vấn đề liên quan