簡體   English   中英

使用 Python 使用 Flask 將文檔上傳到 Dropbox API

[英]Using Python to upload document to Dropbox API with Flask

嘗試通過 Flask 應用程序上的Submit按鈕通過 Dropbox 的 API 上傳文檔。 HTML 在 localhost 上加載,但每當我上傳文檔並點擊Sumbit時,都會出現404錯誤,並且文檔不會發布到 Dropbox API。 關於我哪里出錯的任何想法?

Python

from flask import Flask, render_template, request
import dropbox

# Function Definition
def uploader(token, file):
   target = '/temp'
   targetFile = target + 'test.docx'
   connection = dropbox.Dropbox(token)
   meta = connection.files_upload(file, targetFile, mode=dropbox.files.WriteMode("overwrite"))

# Flask App
app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def upload_document():
    if request.method == "POST":
        uploader(token, request.files['file'])
    return render_template('index.html')

if __name__ == "__main__":
    app.run()

HTML

<!DOCTYPE html>
<html>
    <head>
    </head>
        <body>
            <form method = "post" action = "/home" enctype = "multipart/form-data">
                <p>
                    <input type="file" name="file" autocomplete="off" required>
                </p>
                <p>
                    <input type="submit" value="Submit">
                </p>
            </form> 
        </body>
</html>

看起來問題源於腳本在通過 Dropbox 連接傳遞函數時未讀取文件。 使用它時,在連接中添加file.read()

# Function Definition
def uploader(token, file):
   target = '/temp'
   targetFile = target + 'test.docx'
   connection = dropbox.Dropbox(token)
   meta = connection.files_upload(file.read(), targetFile, mode=dropbox.files.WriteMode("overwrite"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM