0

I am developing SharePoint Office 365 App in visual studio. I have used a FileUpload control to upload files to SharePoint document library by using CSOM.

I am facing an issues. The SharePoint Office 365 App doesn't allow to upload file greater than 3 MP in my custom page and showing the following error message.

Maximum request length exceeded.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Maximum request length exceeded.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): Maximum request length exceeded.]
   System.Web.HttpRequest.GetEntireRawContent() +9726860
   System.Web.HttpRequest.GetMultipartContent() +63
   System.Web.HttpRequest.FillInFormCollection() +165
   System.Web.HttpRequest.EnsureForm() +75
   System.Web.HttpRequest.get_Form() +12
   System.Web.HttpRequest.get_HasForm() +9728411
   System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) +95
   System.Web.UI.Page.DeterminePostBackMode() +69
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +130

Please help me, how i can increase max size of file upload in SharePoint Apps.

Thanks in advance.

Saifal Maluk
  • 63
  • 2
  • 12

2 Answers2

1

The following example demonstrates how to upload file into O365 using File.SaveBinaryDirect method:

public static void UploadFile(Web web,string serverRelativeUrl, string filePath)
{
     using (var fs = new FileStream(filePath, FileMode.Open))
     {
        var fi = new FileInfo(filePath);
        var fileUrl =  String.Format("{0}/{1}", serverRelativeUrl, fi.Name);
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, fileUrl, fs, true);
     }
} 

Usage

//Upload file into Assets library
UploadFile(ctx, "/Assets", @"C:\VideoArchive\VideoSample.mp4");

It works for me when uploading files with size more then 3MB

How to update File properties

//Update Title property for File object
var uploadedFile = ctx.Web.GetFileByServerRelativeUrl(fileRelativeUrl);
uploadedFile.ListItemAllFields["Title"] = "New Title";
uploadedFile.ListItemAllFields.Update();
ctx.ExecuteQuery();
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • Thanks for your reply. One thing i want to add here. Can we add some properties like (Name, Title, or some fields) while saving file. – Saifal Maluk Jun 12 '14 at 10:13
0
 using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(Request.QueryString["SPHostUrl"].ToString(), accessToken))
                {
                    //Get Web and List object
                    Web web = context.Site.OpenWeb("test");
                    List list = web.Lists.GetByTitle("Documents");
                    //Open file
                    System.IO.FileStream stream = System.IO.File.OpenRead(txtFilePath.Text);
                    //Create new file creation info object        
                    FileCreationInformation fileCreation = new FileCreationInformation();
                    fileCreation.ContentStream = stream;
                    fileCreation.Overwrite = true;
                    fileCreation.Url = "test";
                    //Add File to documents library
                    File uploadedFile = list.RootFolder.Files.Add(fileCreation);
                    list.Update();
                    web.Update();
                    context.ExecuteQuery();
                }

Try this solution to upload files.

AkshayP
  • 188
  • 3
  • 11
  • This is similar with my code. Actually when i clicked on upload button the compiler doesn't go to this code and show message first. – Saifal Maluk Jun 12 '14 at 09:06
  • can u help me, how we can change upload file size limit in web.config file of application. – Saifal Maluk Jun 12 '14 at 09:07
  • Try to use : in System.Web section in Web.Config – AkshayP Jun 12 '14 at 09:32
  • This is not allowed in SharePoint Apps web.config. When we add this to web.config the it shows the following error. HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid. – Saifal Maluk Jun 12 '14 at 10:53