简体   繁体   中英

Using Python to upload document to Dropbox API with Flask

Attempting to upload a document through Dropbox's API through a Submit button on Flask application. The HTML loads on localhost, but whenever I upload the document and hit Sumbit , there is a 404 error and the document does not post to the Dropbox API. Any ideas on where I'm going wrong?

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>

It looks like the issue stemmed from the script not reading the file when being passed through the function via the Dropbox connection. When using this, add file.read() within the connection.

# 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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM