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
lovetolearnlovetolearn 

Insert Image from Desktop

I am trying to insert an image onto my VF page.

 

here is what i have:

 

<IMG src="C:\Documents and Settings\*****\My Documents\My Pictures\test.gif" ALT= "alternate_text" />

 

i also tried

 

<IMG src="test.gif" ALT= "alternate_text" />

 

both ways i ended up with the same result, the image didnt display, the image box only displayed the alternate text. please help thanks.

g.engelg.engel

Your browser won't allow a VisualForce page to access files on your Desktop.  You have to upload them to the server by accessing Setup | Develop | Static Resources.  You can zip up several images into one resource file, up to 5MB in size.

 

To access images in your resource file, use the tag:

 

         <apex:image url="{!URLFOR($Resource.zipfile, 'test.gif')}"/> 

 

where 'zipfile' is the name you assigned to the resource zip you uploaded.

Pradeep_NavatarPradeep_Navatar

We have to insert the image in document or Static Resource before displaying  them on VF page.

 

Below is the sample code to insert the images in document through VF page and then display them-

 

-------- VF Page Code --------

<apex:inputFile value="{!propFileBody}" fileName="{!propFileName}" contentType="{!propContentType}" id="iFile"/> // to insert the image in document

<apex:image url="{!URLFOR($Resource.ZipFileName, 'images/vks.png')}" alt="Vivek" /> // to display the image from static resource

<apex:image url="{!VKS_Photo_Url}" alt="Vivek" /> // to display the image from document

 

-------- Apex Controller Code --------

                public String  propFileName{ get; set; }

                public Transient Blob propFileBody{ get; set; }

                public String  propContentType{ get; set; }

 

                Folder fd = [Select id, name From Folder Where name = 'Images'];

                String strOrgId = '';

                strOrgId  = UserInfo.getOrganizationId();

 

                Document doc = new Document();

                doc.Body = propFileBody;

                doc.Name = propFileName;

                doc.ContentType = propContentType;

                doc.Type = propContentType.substring((propContentType.indexOf('/')+1),propContentType.length());

                doc.IsPublic = true;

                doc.FolderId = fd.id;

                Database.SaveResult sr = Database.Insert(doc);

                // above is the code to insert the image in document.

 

String VKS_Photo_Url = '/servlet/servlet.ImageServer?oid=' + strOrgId + '&id='+sr.getId();  // document image Url to directly use on VF page to display image

lovetolearnlovetolearn

 


g.engel wrote:

Your browser won't allow a VisualForce page to access files on your Desktop.  You have to upload them to the server by accessing Setup | Develop | Static Resources.  You can zip up several images into one resource file, up to 5MB in size.

 

To access images in your resource file, use the tag:

 

         <apex:image url="{!URLFOR($Resource.zipfile, 'test.gif')}"/> 

 

where 'zipfile' is the name you assigned to the resource zip you uploaded.


thank you i will try this now.

 

lovetolearnlovetolearn

 


Pradeep_Navatar wrote:

We have to insert the image in document or Static Resource before displaying  them on VF page.

 

Below is the sample code to insert the images in document through VF page and then display them-

 

-------- VF Page Code --------

<apex:inputFile value="{!propFileBody}" fileName="{!propFileName}" contentType="{!propContentType}" id="iFile"/> // to insert the image in document

<apex:image url="{!URLFOR($Resource.ZipFileName, 'images/vks.png')}" alt="Vivek" /> // to display the image from static resource

<apex:image url="{!VKS_Photo_Url}" alt="Vivek" /> // to display the image from document

 

-------- Apex Controller Code --------

                public String  propFileName{ get; set; }

                public Transient Blob propFileBody{ get; set; }

                public String  propContentType{ get; set; }

 

                Folder fd = [Select id, name From Folder Where name = 'Images'];

                String strOrgId = '';

                strOrgId  = UserInfo.getOrganizationId();

 

                Document doc = new Document();

                doc.Body = propFileBody;

                doc.Name = propFileName;

                doc.ContentType = propContentType;

                doc.Type = propContentType.substring((propContentType.indexOf('/')+1),propContentType.length());

                doc.IsPublic = true;

                doc.FolderId = fd.id;

                Database.SaveResult sr = Database.Insert(doc);

                // above is the code to insert the image in document.

 

String VKS_Photo_Url = '/servlet/servlet.ImageServer?oid=' + strOrgId + '&id='+sr.getId();  // document image Url to directly use on VF page to display image


thank you i understand the VF page codes, but im not sure where to go with the controller codes. please elaborate. thank you for your time.

 

lovetolearnlovetolearn

 


Pradeep_Navatar wrote:

 

-------- VF Page Code --------

 

<apex:image url="{!URLFOR($Resource.ZipFileName, 'images/vks.png')}" alt="Vivek" /> // to display the image from static resource


i tried this code after i uploaded my image onto static resouces, i inputted: <apex:image url="{!URLFOR($Resource.NAMEOFTHEUPLOADEDFILE, 'images/NAMEOFTHEUPLOADEDFILE.EXTENSIONOFTHEUPLOADEDFILE')}" alt="alternate_text" />

 

but my VF page didnt display anything.

 

 

 

Pradeep_NavatarPradeep_Navatar

Have you checked the view source option of the code after right clicking on visual force page? Are you getting the correct URL for an image? It may happen that you are not referencing the correct URL where you have saved the image. Check and let me know if there is an issue.

lovetolearnlovetolearn

Pradeep_Navatar wrote:

Have you checked the view source option of the code after right clicking on visual force page? Are you getting the correct URL for an image? It may happen that you are not referencing the correct URL where you have saved the image. Check and let me know if there is an issue.


Let me check. In the meantime, is there anything wrong with my coding?

Jon Mountjoy_Jon Mountjoy_

I see you used  $Resource.NAMEOFTHEUPLOADFILE

 

Note that it should be the name that you gave to the resource. 

 

ie. when you create the resource, you give it a name, say FooBar.

 

When you do, you must write $Resource.Foobar

lovetolearnlovetolearn

Jon Mountjoy_ wrote:

I see you used  $Resource.NAMEOFTHEUPLOADFILE

 

Note that it should be the name that you gave to the resource. 

 

ie. when you create the resource, you give it a name, say FooBar.

 

When you do, you must write $Resource.Foobar


yep, thats exactly what i did.

Jon Mountjoy_Jon Mountjoy_

Okay, if you're still having problems, I suspect it's due to the zip file.  Perhaps you have an intermediate directory, like

foo/images/bar.png, instead of images/bar.png?

 

lovetolearnlovetolearn

thanks for all your help guys. i figure it out: i had to set the cache to public first, which i forgot to do so. thanks again. 

Anil Kumar 1257Anil Kumar 1257
User-added image