Welcome to the new Box Support website. Check out all the details here on what’s changed.

Box Upload API loading the file but the file is unreadable and has no size displayed too

Answered
New post

Comments

7 comments

  • mwiller

     Based on the code you included, it doesn't look like you're actually including the contents of the file in the payload — the string you pasted just has an empty line for the `file` part of the multipart form body.  You'll need to actually add the file contents to upload them.  Have you tried using our Python SDK?  It has methods available to do file upload for you!

    0
    Comment actions Permalink
  • naga1990

    . Thanks for your answer I don't see the upload functionality here --> https://github.com/box/box-python-sdk#usage 

    I don't see the BoxSDK in Anaconda package list too. Is the sdk not available via Anaconda ?  

    0
    Comment actions Permalink
  • naga1990

     , I have already written a lot of code with Python requests library and I have to refactor a lot of code if I have to switch to Box SDK. So I thought of sticking to Python requests library for now

     

    The following question may be naive. But I am kind of stuck with uploading the file.

     

     

    The following payload at least loads a empty file

    payload = 
    "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n
    Content-Disposition: form-data; name=\"attributes\"\r\n\r\n
    {\"name\":\"XYZ.xlsx\", \"parent\": {\"id\": \"XYZ\"}}\r\n
    ------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n
    Content-Disposition: form-data; name=\"file\"; filename=\"C:\\XYZ\\XYZ.xlsx\"\r\n
    Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n\r\n
    ------WebKitFormBoundary7MA4YWxkTrZu0gW--"

     

    But the below payload returns 400 error. I am trying to send the file contents in this payload. What am I doing wrong here ?

     

    payload = {
    	'Content-Disposition': 'form-data',
    	'attributes': {'name': 'XYZ.xlsx', 'parent': {'id': 'XYZ'}},
    	'file': {'name': open('C:\\XYZ\\XYZ.xlsx','rb'),
    	         'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
    }
    0
    Comment actions Permalink
  • mwiller

     Have you checked out the requests library documentation on POSTing files via a multi-part form?  You should be able to follow that pattern to pass a file stream to requests for the file part — manually constructing the payload string is probably not advisable in this case.  You could take a look at what the Box Python SDK is doing to get an idea of one way this could work.  In general, it looks like you may want to use the MultipartEncoder from requests toolbelt to construct a multipart form containing both the file attributes JSON and the file contents.

    0
    Comment actions Permalink
  • naga1990

     , Java SDK for Box is far more easier than Python SDK. I switched to Java SDK.

     

    I hope the Python SDK becomes more developer friendly with clear examples like Java SDK --> https://github.com/box/box-java-sdk

    0
    Comment actions Permalink
  • mwiller

     I'm really sorry you weren't able to get things working with Python!  You're correct though, our Java SDK is currently much better documented.  My team will actually be taking on a project to improve the Python SDK and its documentation in the next few months, so hopefully we can make the situation a bit better going forward.

    0
    Comment actions Permalink
  • hdf

    +1, I am familiar to AWS S3 upload with boto and they have two distinct methods :

     

    1 for the multipart upload with a queue management for multithreading and the other one for the simple single file upload.

     

    The right url for Java is here : https://github.com/box/box-java-sdk/blob/master/doc/files.md#upload-a-large-file-in-chunks

     

    Maybe I can have a look at it.

    0
    Comment actions Permalink

Please sign in to leave a comment.