服务器不允许大于5242880字节的消息

时间:2016-03-15 07:31:06

标签: c# sharepoint

Elmah最近报告了这个错误;

Microsoft.SharePoint.Client.ServerException:请求消息太大。服务器不允许大于5242880字节的消息。

它倒下的代码是;

public SharepointFileInfo Save(byte[] file, string fileName)
{
    using (var context = new ClientContext(this.SharepointServer))
    {
        context.Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain);

        var list = context.Web.Lists.GetByTitle(this.DocumentLibrary);
        var fileCreationInformation = new FileCreationInformation
                                          {
                                              Content = file,
                                              Overwrite = true,
                                              Url = fileName
                                          };
        var uploadFile = list.RootFolder.Files.Add(fileCreationInformation);
        var listItem = uploadFile.ListItemAllFields;
        listItem.Update();

        context.ExecuteQuery();

        if (this.Metadata.Count > 0)
        {
            this.SaveMetadata(uploadFile, context);
        }

        return GetSharepointFileInfo(context, list, uploadFile);
    }
}

我使用的是Sharepoint 2013。 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

这是一个正常的问题。您使用经典API(新的FileCreationInformation [...] context.ExecuteQuery())将HTTP requet发送到服务器。您的文件最高为5 Mb。因此,IIS收到一个巨大的请求,并拒绝它。 要将文件上载到SharePoint,您需要使用:

File.SaveBinaryDirect (使用此功能,您无需更改设置;))

using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
  Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, string.Format("/{0}/{1}", libraryName, System.IO.Path.GetFileName(filePath)), fs, true);
}

检查此链接以了解如何使用CSOM将文件上传到SharePoint: Upload large files sample app for SharePoint

祝你好运

答案 1 :(得分:1)

有几个方法可以做到(使用metaData上传文件)。我向你提出两种方法(一种简单,另一种更复杂)

  1. 2次(简单)

    • 使用File.SaveBinaryDirect上传文件
    • 使用 SP.Web.getFileByServerRelativeUrl File.listItemAllFields 方法,通过文件URL获取带有CSOM的SPFile。 这是一个例子:get listitem by file URL
  2. 使用 FileCreationInformation 但更复杂。 您需要使用: File.StartUpload File.ContinueUpload File.FinishUpload 代码来自Microsoft tuto的最后一部分,而不是我的

    public Microsoft.SharePoint.Client.File UploadFileSlicePerSlice(ClientContext ctx, string libraryName, string fileName,int fileChunkSizeInMB = 3){
    // Each sliced upload requires a unique ID.
    Guid uploadId = Guid.NewGuid();
    
    // Get the name of the file.
    string uniqueFileName = Path.GetFileName(fileName);
    
    // Ensure that target library exists, and create it if it is missing.
    if (!LibraryExists(ctx, ctx.Web, libraryName))
    {
        CreateLibrary(ctx, ctx.Web, libraryName);
    }
    // Get the folder to upload into. 
    List docs = ctx.Web.Lists.GetByTitle(libraryName);
    ctx.Load(docs, l => l.RootFolder);
    // Get the information about the folder that will hold the file.
    ctx.Load(docs.RootFolder, f => f.ServerRelativeUrl);
    ctx.ExecuteQuery();
    
    // File object.
    Microsoft.SharePoint.Client.File uploadFile;
    
    // Calculate block size in bytes.
    int blockSize = fileChunkSizeInMB * 1024 * 1024;
    
    // Get the information about the folder that will hold the file.
    ctx.Load(docs.RootFolder, f => f.ServerRelativeUrl);
    ctx.ExecuteQuery();
    
    
    // Get the size of the file.
    long fileSize = new FileInfo(fileName).Length;
    
    if (fileSize <= blockSize)
    {
        // Use regular approach.
        using (FileStream fs = new FileStream(fileName, FileMode.Open))
        {
            FileCreationInformation fileInfo = new FileCreationInformation();
            fileInfo.ContentStream = fs;
            fileInfo.Url = uniqueFileName;
            fileInfo.Overwrite = true;
            uploadFile = docs.RootFolder.Files.Add(fileInfo);
            ctx.Load(uploadFile);
            ctx.ExecuteQuery();
            // Return the file object for the uploaded file.
            return uploadFile;
        }
    }
    else
    {
        // Use large file upload approach.
        ClientResult<long> bytesUploaded = null;
    
        FileStream fs = null;
        try
        {
            fs = System.IO.File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            using (BinaryReader br = new BinaryReader(fs))
            {
                byte[] buffer = new byte[blockSize];
                Byte[] lastBuffer = null;
                long fileoffset = 0;
                long totalBytesRead = 0;
                int bytesRead;
                bool first = true;
                bool last = false;
    
                // Read data from file system in blocks. 
                while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
                {
                    totalBytesRead = totalBytesRead + bytesRead;
    
                    // You've reached the end of the file.
                    if (totalBytesRead == fileSize)
                    {
                        last = true;
                        // Copy to a new buffer that has the correct size.
                        lastBuffer = new byte[bytesRead];
                        Array.Copy(buffer, 0, lastBuffer, 0, bytesRead);
                    }
    
                    if (first)
                    {
                        using (MemoryStream contentStream = new MemoryStream())
                        {
                            // Add an empty file.
                            FileCreationInformation fileInfo = new FileCreationInformation();
                            fileInfo.ContentStream = contentStream;
                            fileInfo.Url = uniqueFileName;
                            fileInfo.Overwrite = true;
                            uploadFile = docs.RootFolder.Files.Add(fileInfo);
    
                            // Start upload by uploading the first slice. 
                            using (MemoryStream s = new MemoryStream(buffer))
                            {
                                // Call the start upload method on the first slice.
                                bytesUploaded = uploadFile.StartUpload(uploadId, s);
                                ctx.ExecuteQuery();
                                // fileoffset is the pointer where the next slice will be added.
                                fileoffset = bytesUploaded.Value;
                            }
    
                            // You can only start the upload once.
                            first = false;
                        }
                    }
                    else
                    {
                        // Get a reference to your file.
                        uploadFile = ctx.Web.GetFileByServerRelativeUrl(docs.RootFolder.ServerRelativeUrl + System.IO.Path.AltDirectorySeparatorChar + uniqueFileName);
    
                        if (last)
                        {
                            // Is this the last slice of data?
                            using (MemoryStream s = new MemoryStream(lastBuffer))
                            {
                                // End sliced upload by calling FinishUpload.
                                uploadFile = uploadFile.FinishUpload(uploadId, fileoffset, s);
                                ctx.ExecuteQuery();
    
                                // Return the file object for the uploaded file.
                                return uploadFile;
                            }
                        }
                        else
                        {
                            using (MemoryStream s = new MemoryStream(buffer))
                            {
                                // Continue sliced upload.
                                bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s);
                                ctx.ExecuteQuery();
                                // Update fileoffset for the next slice.
                                fileoffset = bytesUploaded.Value;
                            }
                        }
                    }
    
                } // while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
            }
        }
        finally
        {
            if (fs != null)
            {
                fs.Dispose();
            }
        }
    }
    return null;}
    
  3. 希望这能帮到你