+ Start a Discussion
Ravikant kediaRavikant kedia 

Unable to upload file on box.com in readable form .

Hello everyone,
                            I am doing integration salesforce with box.com when i uplod file that time first i convert blob into base 64 encoded string then send file using rest api after uploading  file my file in  not readable form it meanse file is encoded. And i am sending file in body as a blob and also try in body but i can't sucess. so give me some idea that how i can solve this issue.
   (Thanks in advance).
             
Best Answer chosen by Ravikant kedia
Deepak Kumar ShyoranDeepak Kumar Shyoran
Box dot supports some common formate like csv,txt,etc which contains text type data, and you'll be easily able to do that but when you try to upload some file (Blob type) then you need to some work around for that.

Try below code it'll help you to upload your file to box.com
public static HTTPResponse uploadFile(Attachmnet file)
 {
  String boundary = '__boundary__xxx';
  String header = '--'+boundary+'\n';
     + 'Content-Disposition: form-data; name="data"; filename="'+file.name
     +'"\nContent-Type: application/octet-stream\n\n';
 
  String footer = '\n--'+boundary+'--';
   
  String body = EncodingUtil.base64Encode(file.Body); //encodes the blob into a base64 encoded String
   
  body = header + body + footer;
   
  HttpRequest req = new HttpRequest();
  req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
  req.setMethod('POST');
  req.setEndpoint('http://posttestserver.com/post.php?dir=what_a_wonderful_post');   //COOL site to test form uploads
  req.setBody(body);
  req.setTimeout(60000);
  req.setHeader('Content-Length',String.valueof(body.length()));
   
  Http http = new Http();
      return http.send(req);
 }

Please mark my answer as a best solution to your question to help others if it solves your problem.

All Answers

AshwaniAshwani
Look at the following link:

http://enreeco.blogspot.it/2013/01/salesforce-apex-post-mutipartform-data.html

You can not send a file to server which is expecting a binary. So you need to encode the base64 in binary supported format. Above blog post is all about that.
Deepak Kumar ShyoranDeepak Kumar Shyoran
Box dot supports some common formate like csv,txt,etc which contains text type data, and you'll be easily able to do that but when you try to upload some file (Blob type) then you need to some work around for that.

Try below code it'll help you to upload your file to box.com
public static HTTPResponse uploadFile(Attachmnet file)
 {
  String boundary = '__boundary__xxx';
  String header = '--'+boundary+'\n';
     + 'Content-Disposition: form-data; name="data"; filename="'+file.name
     +'"\nContent-Type: application/octet-stream\n\n';
 
  String footer = '\n--'+boundary+'--';
   
  String body = EncodingUtil.base64Encode(file.Body); //encodes the blob into a base64 encoded String
   
  body = header + body + footer;
   
  HttpRequest req = new HttpRequest();
  req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
  req.setMethod('POST');
  req.setEndpoint('http://posttestserver.com/post.php?dir=what_a_wonderful_post');   //COOL site to test form uploads
  req.setBody(body);
  req.setTimeout(60000);
  req.setHeader('Content-Length',String.valueof(body.length()));
   
  Http http = new Http();
      return http.send(req);
 }

Please mark my answer as a best solution to your question to help others if it solves your problem.
This was selected as the best answer