function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dkccraigdkccraig 

Uploading Documents

Hi all,

 

I am trying to upload a file as a Document with apex.

 

Let's say I have a 3 second video clip on my hard drive at "/Users/MyUserName/Desktop/test.avi" and I want to upload this file as a Document.  I have the following code to do so:

 

Document d = new Document;

d.Name = 'Test Video';

d.Body = Blob.valueOf('/Users/MyUserName/Desktop/Test.avi');

d.FolderID = ...; //A valid folder id

insert d;

 

Unfortunately it just sets the body to the blob "/Users/MyUserName/Desktop/test.avi" instead of uploading the file at that path.  What am I doing wrong?

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

You seem to be forgetting that apex runs server side, and therefore can't magically grab random content from your local desktop.

 

The easiest (programatic) way to create documents from a client machine, is via the regular web services API, using the enterprise WSDL, its as easy as reading in the file as an array of bytes, constructing a Document object and calling create. 

All Answers

SuperfellSuperfell

You seem to be forgetting that apex runs server side, and therefore can't magically grab random content from your local desktop.

 

The easiest (programatic) way to create documents from a client machine, is via the regular web services API, using the enterprise WSDL, its as easy as reading in the file as an array of bytes, constructing a Document object and calling create. 

This was selected as the best answer
LBartonLBarton

OK, I am creating a web service which will take in various parameters to then create the attachment.  I am not sure

how to declare the body of the attachment.

 

I have:

WebService public static boolean CreateEmailAttachment(ID AccOppID, string sName, string sBody, integer iBodyLength, string sContentType, ID CreatedByID){

 

I don't think that the body should be string but I am not sure what to declare it.  I know I have to convert the body of an attachment to a

byte array but how do I make sure the text in the attachment will be correct and not a string of hex?

SuperfellSuperfell

It should be a Blob, although i don't see the point of creating a new service for this when you can do the same thing easily via the existing enterprise or partner apis. 

LBartonLBarton

This is the situation.  The users will select attachments that they want to upload.  The information from the

attachments will be put into a SQL table and uploaded to SF every fifteen minutes using a windows service that

will call a web service.  Now is there a more efficient way to do this?  Users will be selecting lots of attachments!

 

 

I didn't want to upload each attachment separately.  Is there another way to do this?

 

 

 

SuperfellSuperfell
Use the enterprise or partner APIs you'll be able to bulk create attachments.
LBartonLBarton

Thanks for your patience.  I have been studying the enterprise .wsdl file and am not sure what you are specifically thinking about.  I would appreciate a little more help please!