我正在尝试使用下面的代码将文件从本地上传到SharePoint。但不知何故,这个错误信息被抛出,让我想知道原因:
Exception calling "UploadFile" with "3" argument(s):
"An exception occurred during a WebClient request."
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,CopyFileToSharePoint.ps1
有人有什么想法吗?
代码
#Copy the file to the destination on SharePoint using WebClient
$SourceFile = $SourcePath + "\" + $FileName
$DestinationFile = $DestinationPath + "/" + $FileName
$client = new-object System.Net.WebClient
$client.UseDefaultCredentials=$true
if ( -not (Test-Path $DestinationPath) ) {
New-Item $DestinationPath -Type Directory | Out-Null
}
$result |% {
Write-Host "Uploading $SourceFile to $DestinationFile"
try{
$client.UploadFile($SourceFile, $DestinationFile)
}
catch{
#one simple retry...
try{
$client.UploadFile($SourceFile, $DestinationFile)
}
catch{
write-error "Failed to upload $SourceFile, $_"
}
}
}
答案 0 :(得分:1)
解决!最终似乎有用的是设置contenttype:
$client.ContentType = "application/octet-stream";
以某种方式解决了这个问题。 有想法尝试从这个线程: Error using HttpWebRequest to upload files with PUT
答案 1 :(得分:1)
Also, the file you uploading should be 'Checked Out' in the SharePoint Library before run the UploadFile command. If not, UploadFile throws the above MethodException error of 3 arguments. I recreated this error manually toggling the SharePoint Library settings 'Allow Documents to be checked out before editing?'. But I don't know if we can do the check out/in using System.Net.WebClient programatically. (Din't find anything in the Documentation though)