Reputation: 6158
I am using SalesforceMobileSDK-Android to develop an android application. I am able to develop a very basic android application, In my application, I am able to fetch the contact,account,lead etc details from the salesforce account and perform crud operation on these data. In my android application,I have a button called uploadFile, Now want to upload an audio file on click of that button, I am not able to find any rest api which will help me to upload it on Salesforce from my android client application.
If there is any sample url or source code or any helpful resource please provide me.
Thanks
Upvotes: 0
Views: 1062
Reputation: 19612
You'll have to experiment with base64
encoding the file and sending a POST
request to /services/data/v26.0/sobjects/attachment/{parent record id}/body
endpoint. I haven't done it myself but there are some nice examples:
Upvotes: 1
Reputation: 496
It's mostly server side you have to be concerned about when uploading files, client side you could have an approach like this (just get the idea, it's not a full functional code):
FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name:\"uploadedfile\";filename=\"" + selectedPath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Debug","File is written");
fileInputStream.close();
dos.flush();
dos.close();
Upvotes: 0