1

I was wondering if it is possible to upload a file directly to be used with the Box View API without saving it on my local server. The idea is users will be able to upload a file (pdf, ppt. etc) and it will be used only with the Box Viewer and not saved in my server. Users will be uploading many large files and I am looking to avoid storing them.

I know Box requires a URL of the file location for it to generate the content, but is there a way for the file to be uploaded and handled with the View API?

If anyone knows of a solution it will be greatly appreciated! Thanks.

2 Answers2

1

There's no way to upload directly from the user's browser, because the API does not supply CORS headers. This is for security reasons, because in order to upload directly from the client, you'd have to expose your API token (which you definitely do not want to do).

One way to not store the files on your server would be to essentially proxy a multi-part upload request to the View API (see this gist for an example of how to do it with node.js). The other option would be to use a service such as FilePicker, which allows users to select files from their own computer or any number of other services, and it just returns a URL that you can simply pass to the View API using the URL upload.

lakenen
  • 3,436
  • 5
  • 27
  • 39
  • Thanks for the reply. So is there no way to call the View API without passing a URL of the file? Say I use base64 format for example to achieve the same result, is there any feature of Box that will support this? – Constantinos Crecoukias May 21 '15 at 19:51
  • You can upload a file via multi-part POST: https://box-view.readme.io/#documents-2 (note the different URL: https://upload.view-api.box.com). – lakenen May 21 '15 at 21:09
  • Cool this might work for me, thanks. I am using ASP.NET, and i have the bytes of a file. What do I need to convert the bytes to in order to do a multipart upload? – Constantinos Crecoukias May 22 '15 at 13:11
  • I'm not familiar with ASP.NET, but a quick google search turned this up: http://benfoster.io/blog/web-api-multipart-file-upload-additional-form-data – lakenen May 22 '15 at 17:37
  • @ConstantinosCrecoukias does this answer your question? – lakenen May 27 '15 at 15:00
  • Yes thanks. I am doing research in how to use the multi-part upload in ASP.NET. Hopefully that will solve my issue. – Constantinos Crecoukias May 27 '15 at 19:54
0

The multi-part API is only an option if you are sending large files. Here's a response from trying to send a small file:

{"data":
{"code":"file_size_too_small",
"message":"File size 23 less than minimum allowed for this API: 20000000",
"request_id":"aab58e965e91c8aa7283b2faddec5ab3"},
"status":400,"config":{"method":"POST",
"transformRequest":[null],
"transformResponse":[null],
"jsonpCallbackParam":"callback",
"url":"https://upload.box.com/api/2.0/files/upload_sessions",
"headers":{"Authorization":"Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Content-Type":"multipart/form-data",
"Accept":"application/json, text/plain, */*"},
"data":{"folder_id":"111111111111",
"file_size":23,
"file_name":"TestUploadFile.txt"}},
"statusText":"Bad Request",
"xhrStatus":"complete"}
MangoCat
  • 89
  • 5