Sharepoint 上传文件 'Specified argument was out of range of valid values'

Sharepoint UploadFile 'Specified argument was out of range of valid values'

我正在尝试将文件从一个库复制到远程事件接收器中的另一个库。

当到达 UploadFile 函数时,出现以下错误:

Specified argument was out of the range of valid values. Parameter name: bytesToCopy

可以找到有关函数的信息here。它具有以下签名:

public static Microsoft.SharePoint.Client.File UploadFile (this Microsoft.SharePoint.Client.Folder folder, string fileName, System.IO.Stream stream, bool overwriteIfExists);

我的代码是:

using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
    ClientResult<Stream> data = curItem.File.OpenBinaryStream();
    clientContext.ExecuteQuery();

    if (data != null && data.Value != null)
    {

         data.Value.CopyTo(stream);
         UploadedFile = destinationList.RootFolder.UploadFile(curItem.File.Name.ToString(), stream, true);
    }
}

注意:curItemdestinationListcurItem.File 都已加载到上下文中。

编辑,完成错误:

   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext clientContext, Int32 retryCount, Int32 delay, String userAgent)
   at Microsoft.SharePoint.Client.FileFolderExtensions.UploadFile(Folder folder, String fileName, Stream stream, Boolean overwriteIfExists)
   at PG.SHP.SCE.QCSPAddInWeb.Services.AppEventReceiver.HandleItemCheckedIn(SPRemoteEventProperties properties) in C:\Users\OBJ_JDWR\Source\Workspaces\SharePoint\SCE\Main\Source\PG.SHP.SCE\Source\PG.SHP.SCE.QCSPAddInWeb\Services\AppEventReceiver.svc.cs:line 288

第 288 行:

如何消除错误?

快速 google 搜索将我带到 this article

有人遇到了类似的问题(尽管他认为这与文件大小有关)并通过在将其发送到 SharePoint 之前重置流来解决它:

using (var stream = content)
{
    ...
    stream.Seek(0, SeekOrigin.Begin); // <-- The missing statement
    ...
}

希望对你也有用:)