+ 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
Test Dev 81Test Dev 81
hii ,i
tried the above example but getting an internal server error 500
this is my code
public static void addAttachments(List<Opportunity>oppList){
        List<Attachment>opportunityAttach = new List<Attachment>();
        for(Opportunity opp : oppList){
       
            //Normal Text
            String bodyStr;
            bodyStr='Opportunity Name'+'|'+'Stage'+'|'+'Opportunity Id'+'\n';
            bodyStr+= opp.name+'|'+opp.StageName+'|'+opp.Id;
            Attachment att=new Attachment();
            att.Body=Blob.valueOf(bodyStr);
            System.debug( att.Body);
            //set file  ext
            att.Name= opp.Name+'_' +System.now().format('yyyy_MM_dd_hh_mm_ss') + '.txt';
            att.parentId=opp.id;
            HTTPResponse htt = uploadFile(att);
            System.debug('resp '+htt);
            System.debug('resp '+htt.getBody());
            //SYSTEm.debug('ATTACHMENTS IS '+att);
        }
        // insert opportunityAttach;
    }
    public static HTTPResponse uploadFile(Attachment file) {
        String boundary = '__boundary__xxx';
        String header = '--'+boundary+'\n';
        header += '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('https://saas.docufree.com/dfwebservice/v3/files/capture/folder?targetId=3486');
        req.setBody(body);
        req.setTimeout(60000);
        Rg_FileUploadService obj = new Rg_FileUploadService();
        string accessToken = obj.genAccessToken();
        
        req.setHeader('Authorization','Bearer ' + accessToken);
        req.setHeader('Content-Length',String.valueof(body.length()));
        
        Http http = new Http();
        return http.send(req);
    }

can you please tell me what I am doing wrong here?
Thanks,
Rahul
sje lseksje lsek
Please share the complete scirpt.  I want to use it for my new page that hosted on WordPress. You can see here (https://nextgenapk.com/hotspot-shield-premium-apk/) page detail.