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
RedSalesRedSales 

Force.Ccom Site - "Authorization Required" Error after attaching A Large File

Hi,  I'm troubleshooting an error users are having withn a force.com web form.   The scenario is as follows:

 

- A user accesses the Force.Com site page

- He/She has many form fields, one of which is a field to browse to a file and to upload/attach this file.

- After selecting the file and hitting "Attach" the web form page refreshes and displays the filename of the file attached.  The file has been attached to the documents folder within SFDC.

 

- What is happening for larger files is that on clicking "attach"  the file is being uploaded to the Documents folder but the user receives an error message as follows in the web page.

 

"Authorization Required

You must first log in or register before accessing this page.
If you have forgotten your password, click Forgot Password to reset it. "

 

The page is a public facing page (not requiring any logins/authorization).  For smaller attachments the page refreshes ok with the new filename added after an attachment is made.  Do you know if there may be settings which is causing this error? As the file attaches/uploads ok to the documents folder I'm guessing there is no issue wtih filesize restrictions.

 

If you have any suggestons/thoughts on this and help would be appreciated.

 

Thanks! 

Best Answer chosen by Admin (Salesforce Developers) 
cvuyyurucvuyyuru

A visual force page has some view state.

 

It states that inorder to view data on screen, it holds limited size of data.

 

the Maximum view state of a visual force page is 135 KB(Before it is 128 KB only).

 

coming to  your problem.

 

 

In the code. 

 

You will be using a variable to hold file data like this.

 

<apex:inputFile value="{!file}"/>

 

In Apex class:

 

public blob file{get; set;}

 

 

Use transient keyword before blob data type.

 

like this

 

public transient blob file{get; set;}

 

 

The transient keyword gives persistency in the VF page..

 

 

 

Hope it solves your problem.

 

 

 

 

All Answers

cvuyyurucvuyyuru

Try in your sandbox bby uploading large files.

 

I think you are getting View State problem.

 

 

RedSalesRedSales

Hi Charam, thanks for your feedback.  I have tried this out on our sandbox also and the same issue occurs.

By "View State" problem, what do you mean?

 

Thanks,

cvuyyurucvuyyuru

View state something that a current page can hold.

 

 

I mean try to upload a large file in VF page not in sites(In Sandbox).

 

 

What issue you are getting exactly while uploading the file iN VF page in sandbox(not in site page)?

RedSalesRedSales

Hi Charam,

 

Thanks for your suggestions.  I opened the visual force page (without going through sites)  and receive the following error messages when trying to upload two files

 

Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 156.516KB  

 

first of all.

 

Then

 

Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 138.969KB 

 

This therefore does seem to be a View State problem as you have mentioned.  I've had a look at the following article on View States 

 

http://wiki.developerforce.com/page/An_Introduction_to_Visualforce_View_State

 

What I find unusual is that this error must not have been occuring previously. Do you know what may be causing such an error now  & if there is a way to increase the viwe state size configurably without having to change the visualforce page or controller class code?

 

Thanks!

cvuyyurucvuyyuru

A visual force page has some view state.

 

It states that inorder to view data on screen, it holds limited size of data.

 

the Maximum view state of a visual force page is 135 KB(Before it is 128 KB only).

 

coming to  your problem.

 

 

In the code. 

 

You will be using a variable to hold file data like this.

 

<apex:inputFile value="{!file}"/>

 

In Apex class:

 

public blob file{get; set;}

 

 

Use transient keyword before blob data type.

 

like this

 

public transient blob file{get; set;}

 

 

The transient keyword gives persistency in the VF page..

 

 

 

Hope it solves your problem.

 

 

 

 

This was selected as the best answer
RedSalesRedSales

Hi Charan,

 

Thanks again.  

 

I note in the Apex class there are lines of code as follows:

 

 public List<Document> lstDoc {set;get;}
 public String selectedAttId {set;get;}

 

There is no Blob object but I guess I can add transient to the above also (the LIST/String) as they use get/set?

 

I'll try this out.  It may be a few days before I get round to doing so as I have to run this by others first to make sure its ok for me to change the class.  After which I will update the forum post with how I get on.

 

Thanks again for all your help.  Much appreciated!

 

 

 

RedSalesRedSales

Hi Charan,

 

I changed my code to add the transient setting.  The code is as follows:

 

public transient List<Document> lstDoc {set;get;}
public String selectedAttId {set;get;}

 

public PageReference attachFile(){
  if(lstDoc == NULL)
    lstDoc = new List<Document>();

    if(doc.name!=null){
      doc.folderId = '012420000040Lis';
      insert doc;
      lstDoc.add(doc);
      doc = new Document();
  }
return null;
}

 

When I attach a file the "Authorization Required" Error no longer occurs, however there is a problem.  The VisualForce page (prior to this change) allows more than one attachment to be added/uploaded.  With the change I have made, each time an attachment is added the previous one is deleted.  I guess therefore adding transient to the list means it can't keep it's state somehow when more than one attachment is added.

Making "selectedAttId" transient does not seem to have any impact and I cannot set "lstDoc = new List<Document>();" to "lstDoc = new transient List<Document>();" as it causes an error in saving the class.

 

If you have any suggestions on how I can keep the List object in a way that more than one attachment can be added then this would be appreciated also.

 

Thanks again!

 

RedSalesRedSales

Hi,  I tired the following to see if it would resolve my attachments issue (Note I've added some more of the Apex code also).

 

public transient List<Document> lstDocTemp {set;get;}
public String selectedAttId {set;get;}

public PageReference attachFile(){
if(lstDocTemp == NULL)
lstDocTemp = new List<Document>();

if(doc.name!=null){
doc.folderId = '012420000040Lis';
insert doc;
lstDocTemp.add(doc);
doc = new Document();
}
return null;
}


public List<Document> lstDoc = new List<Document>(lstDocTemp);


public void saveLeadAttachments(String objleadId){
List<Attachment> lstAtt = new List<Attachment>();
for(Document d:lstDoc){
lstAtt.add(new Attachment(Name=d.Name,ParentId=objleadId,Body=d.Body));
}
if(!lstAtt.isEmpty()){
insert lstAtt;
delete lstDoc;
}
}

 

 

The following error now occurs. "Unknown property 'LeadStandardController.lstDoc'"

 

Do you know how I can use a transient keyword to allow the ViewState not to be exceeded when attaching large files but keep my List object that it's not reset each time I do add an attachment.  I wish to add multiple files therefore keep adding to the list object each time.  I tired to create a second List and assign it to the existing transient one but this caused the 'LeadStandardController.lstDoc'" error above.

 

Thanks in advance for any suggestions!

 

RedSalesRedSales

Hi All,

 

The following article provided by Premier Support has helped to resolve my issue.

 

http://developer.force.com/cookbook/recipe/uploading-a-document-using-visualforce-and-a-custom-controller

 

There is no requirement to add the "transient" keyword to my code. By adding "doc.body = null;" the viewstate errors stopped.

 

Thanks all for your help on this.  I'll accept Charan's previous suggestion as the answer to the question as it did initially resolve my View State error, despite my subsequent attachment problem.

 

Thanks!