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
Aaron2010Aaron2010 

RE: pdf / text file/ jpeg etc upload

Can we upload a file from visualforce sites into salesforce? Can anyone tell me the procedure / provide the sample code.

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
_Prasu__Prasu_

You can find the sample code here

All Answers

_Prasu__Prasu_

You can find the sample code here

This was selected as the best answer
Pradeep_NavatarPradeep_Navatar

Try out the sample code given below :

 

VF code:

 

                         <apex:form >

                                <apex:pageBlock title="Upload New Attachment">

                                <table>

                                   <tr>

                                       <td class="rows" >Browse For File</td>

                                        <td><apex:inputFile value="{!filebody}" fileName="{!filename}"  /></td>

                                    </tr>

                                     <tr>

                                     <td></td>

                                      <td align="left"><apex:commandButton action="{!uploadfile}" value="Upload"/></td>

                                    </tr>

                                   </table>

                               </apex:pageBlock>

                          </apex:form>

 

Controller code:

                                                public string parentid { get; set; }

                                                public string filename { get; set; }

                                                public blob   filebody { get; set; }

                                                public void uploadfile()

                                                {

                                                                if(filebody != null && filename != null && filename != '')

                                                                {

                                                                                transient Attachment a = new Attachment();

                                                                                a.body = filebody;

                                                                                a.name = filename;

                                                                                a.parentid = ApexPages.currentPage().getParameters().get('id');

                                                                                try

                                                                                {

                                                                                                insert a;

                                                                                                filename = '';

                                                                                                filebody = null;

                                                                                }

                                                                                catch(Exception e)

                                                                                {

                                                                                }

                                                                }

                                                }

 

Hope this helps.

Aaron2010Aaron2010

Thank you all for your replies. I will try to implement the code that you have provided and will let you know.

 

Again Thank's a lot.