cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Organizing your research is important to easily review and share it. Learn how Kim uses Dropbox to gather and organize her research right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
1
Ask
2
Comments

Uploaded Files are Currupt (Flask, Python)

Uploaded Files are Currupt (Flask, Python)

jmccolgan93
Explorer | Level 3

Hey, guys so I'm experimenting with the Dropbox python API using Flask. I managed to upload files but all the files I've uploaded are only 16 bytes in size and are unable to be opened. here's the code I'm using. 

 

HTML Form:

 

<div style="text-align: center" class="form">
          <form class="needs-validation" novalidate action="/uploadtest" method="POST" enctype="multipart/form-data">
            <div class="col-md-12">
              <div class="form-group">
                <label for="name">Choose file</label>
                <input type="file" accept=".xlsx, .jpg" name="file" id="fileToUpload" class="form-control" placeholder="John Smith"
                  required>
                <div class="valid-feedback">
                  Looks good!
                </div>
                <div class="invalid-feedback">
                  Please choose a .xlsx file.
                </div>
              </div>
            </div>
            <button type="submit" class="btn btn-success" value="">Submit</button>
          </form>
        </div>

 

 

Python Flask Route 

@app.route('/uploadtest', methods=['POST', 'GET'])
def get_dropbox_files():
    if request.method == 'POST':
        uploadedfile = request.files['file']
        client = dbx_client
        f = secure_filename(uploadedfile.filename)
        path="/nameinlights/" + f
        dbx_client.files_upload(f.encode(), path)
        flash("Added" + " " + f + " " + "to Name in Lights Folder", 'success')

            
    return render_template('uploadtest.html', file_list=file_list)

 

1 Accepted Solution

Accepted Solutions

Re: Uploaded Files are Currupt (Flask, Python)

TaylorKrusen
Dropboxer

Hi!

 

When you call file_upload, you're actually uploading the file's name as a string. Each file is coming out to 16 bytes because the strings are being generated by secure_filename and are probably a standard length.

 

You can fix it by changing the first variable you're passing to file_upload.

        dbx_client.files_upload(f.encode(), path)

should be

        dbx_client.files_upload(uploadedfile.encode(), path)

It looks like you're just working with the filename 

View solution in original post

2 Replies 2

Re: Uploaded Files are Currupt (Flask, Python)

TaylorKrusen
Dropboxer

Hi!

 

When you call file_upload, you're actually uploading the file's name as a string. Each file is coming out to 16 bytes because the strings are being generated by secure_filename and are probably a standard length.

 

You can fix it by changing the first variable you're passing to file_upload.

        dbx_client.files_upload(f.encode(), path)

should be

        dbx_client.files_upload(uploadedfile.encode(), path)

It looks like you're just working with the filename 

Re: Uploaded Files are Currupt (Flask, Python)

jmccolgan93
Explorer | Level 3

thanks man! i had to change "encode()" for "read()" thanks for the help man! 

 

 

dbx_client.files_upload(uploadedfile.read(), path)
Who's talking

Top contributors to this post

  • User avatar
    jmccolgan93 Explorer | Level 3
  • User avatar
    TaylorKrusen Dropboxer
What do Dropbox user levels mean?
Need more support?