简体   繁体   English

使用图形 API 将大文件上传到 SharePoint Online - ContentType 错误

[英]Upload large file to SharePoint Online using Graph API - ContentType error

I am looking for a way to upload large files (over 4MB) to SharePoint Online using PowerShell and Graph API.我正在寻找一种使用 PowerShell 和 Graph API 将大文件(超过 4MB)上传到 SharePoint Online 的方法。 I am able to upload files <4MB in size without any problems, but I am struggling with larger files.我可以毫无问题地上传 <4MB 的文件,但我在处理更大的文件时遇到了困难。

I am trying to use this solution but the following part (line 105, when trying to upload the file) is not working for me:我正在尝试使用此解决方案,但以下部分(第 105 行,尝试上传文件时)对我不起作用:

$invokeRestMethodParams = @{
    Uri     = $uploadUrl
    Method  = "PUT"
    Headers = @{
        Accept           = "application/json"
        "Content-Type"   = "text/plain"
        Authorization    = "bearer $($token)"
        "Content-Length" = $fileLength
        "Content-Range"  =  "bytes 0-$($fileLength-1)/$($fileLength)"
    }
    Body = $fileInAscii
}

$response = Invoke-RestMethod @invokeRestMethodParams

I keep getting the following error on the Invoke-RestMethod (everything else before that works fine):我在Invoke-RestMethod上不断收到以下错误(之前的一切都很好):

The cmdlet cannot run because the -ContentType parameter is not a valid Content-Type header. Specify a valid Content-Type for -ContentType, then retry. To suppress header validation, supply the -SkipHeaderValidation parameter.

If I use the -SkipHeaderValidation parameter, I get the following error:如果我使用-SkipHeaderValidation参数,我会收到以下错误:

Response status code does not indicate success: 400 (Bad Request).

I've tried different types of files (they are all less than 10MB) like TXT or PDF, and even different ContentTypes like "application/octet-stream" or "text/plain", but nothing seems to work... I've also tried another solution I found online ( this one ), but the error was the same.我尝试过不同类型的文件(它们都小于 10MB),比如 TXT 或 PDF,甚至不同的 ContentType,比如“application/octet-stream”或“text/plain”,但似乎没有任何效果......我我还尝试了我在网上找到的另一种解决方案( 这个),但错误是一样的。

Is anyone else experiencing the same, or do you have a script to upload large files (over 4MB) to SharePoint Online using PowerShell AND Graph API?是否有其他人遇到相同的情况,或者您是否有使用 PowerShell 和 Graph API 将大文件(超过 4MB)上传到 SharePoint Online 的脚本? Thank you so much!太感谢了!

Regards, Nuno问候,努诺

I've managed to find a simpler solution that works for files over 3MB (I've also tested 100MB size files).我设法找到了一个更简单的解决方案,适用于超过 3MB 的文件(我还测试了 100MB 大小的文件)。 To get the upload URL, I used the code from the solution in my first post.为了获取上传 URL,我在第一篇文章中使用了解决方案中的代码。

    $fileInBytes = [System.IO.File]::ReadAllBytes($file)
    $fileLength = $fileInBytes.Length

    $invokeRestMethodParams = @{
        Uri     = $uploadUrl
        Method  = "PUT"
        Body    = $fileInBytes
        Headers = @{
            'Content-Range' = "bytes 0-$($fileLength-1)/$fileLength"
        }
    }

    Try {
        $response = Invoke-RestMethod @invokeRestMethodParams -ErrorAction Stop
        Write-Log -Type "INF" -Message "File uploaded to SharePoint Online: '$fileName' ($([Math]::Round($file.length/1MB, 0))MB)"
    } Catch {
        Write-Log -Type "ERR" -Message "Unable to upload '$fileName' to SharePoint Online: '$($_.Exception.Message)'"
    }

do you, maybe, know what should I do to get rid of this error: Exception calling "ExecuteQuery" with "0" argument(s): "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."你知道我应该怎么做才能摆脱这个错误:使用“0”参数调用“ExecuteQuery”的异常:“无法从传输连接读取数据:现有连接被远程强制关闭主持人。”

I am uploading files also 10GB big.. usually encounter the above error after 5GB... Any idea?我正在上传文件也是 10GB 大.. 通常在 5GB 后遇到上述错误......知道吗? I also came across possible issue which could be related to our company VPN.. I already included commands like: [System.Threading.Timeout]::Infinite [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Ty for any help..我还遇到了可能与我们公司 VPN 相关的问题。我已经包含了如下命令:[System.Threading.Timeout]::Infinite [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Ty任何帮助..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SharePoint 使用 MS Graph 进行大型上传 API - 第二块出错 - SharePoint Large Upload using MS Graph API - Erroring on second chunk 使用 Microsoft Graph 在线上传文件到 SharePoint API - Uploading a File to SharePoint Online using Microsoft Graph API 使用PowerShell使用元数据将文件上载到SharePoint Online - Upload file to SharePoint Online with metadata using PowerShell 如何使用 PowerShell 在线将文件上传到 SharePoint 中的中心站点 - How to upload a file to a hub site in SharePoint Online using PowerShell 使用REST API将文件上传到SharePoint 2013 - File Upload to SharePoint 2013 using REST API 使用 Powershell 将文件上传到 Sharepoint Online (Microsoft 365)(选项 4 - 使用 Microsoft.SharePoint.Client.ClientContext) - Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 4 - Using Microsoft.SharePoint.Client.ClientContext) 使用 Powershell 将文件上传到 Sharepoint Online (Microsoft 365)(选项 2 - 使用 Microsoft.SharePoint.PowerShell) - Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 2 - Using Microsoft.SharePoint.PowerShell) 尝试使用 powershell 访问 sharepoint 在线列表 REST API 时出错 - Error trying to access the sharepoint online list REST API using powershell 使用 Powershell(选项 1 - 使用 PnP.Powershell)将文件上传到 Sharepoint Online (Microsoft 365) - Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 1-Using PnP.Powershell) 使用PowerShell和OData API将文件上载到SharePoint 2010 - Upload file to SharePoint 2010 using PowerShell and the OData API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM