1

The powershell code to download and upload file from Sharepoint is throwing error for both download and upload functions. Error is : Exception calling "UploadFile" with "3" argument(s): "The request was aborted: The request was canceled."

Any idea what is the cause of this?

FYI, I am able to download file from SharePoint manually!

$SPFileUrl = $SPCodeDropCommanPath + $ApplicationFolder + "/" + "$SPFileName"
$DownloadLocalPath = [System.IO.Path]::Combine("$WorkingDir", "$SPFileName")

$SecurePassword = $SPPassword | ConvertTo-SecureString -AsPlainText -Force
$client = New-Object System.Net.WebClient 
$client.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SPUsername, $SecurePassword)
$client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
try
{
    $client.DownloadFile($SPFileUrl, $DownloadLocalPath)
}
catch
{
    Write-Message $LogFileName "`nDownload file failed"
    $ErrorMessage = $_.Exception.Message
    Write-Message $LogFileName "`n $ErrorMessage"
}
$client.Dispose()

As I said the above code is returning error Exception calling "UploadFile" with "3" argument(s): "The request was aborted: The request was canceled."

And as a solution, from the websites below code was tried

$timeoutWebclientCode = @"
using System;
using System.Net;

public class ExtendedWebClient : WebClient
{
    public int Timeout { get; set; }
    public new bool AllowWriteStreamBuffering { get; set; }
    protected override WebRequest GetWebRequest(Uri address)
    {
    var request = base.GetWebRequest(address);
    if (request != null)
    {
        request.Timeout = Timeout;
        var httpRequest = request as HttpWebRequest;
        if (httpRequest != null)
        {
            httpRequest.AllowWriteStreamBuffering = false;
        }
    }
    return request;
}

public ExtendedWebClient()
{
    Timeout = 100000; // the standard HTTP Request Timeout default
}
 }
"@;

And the above also not working by giving the same error.

Invoke-Webrequest is next thing I was trying on

Invoke-WebRequest -Uri "DownloadURL" -OutFile "LocalPath" -Credential $cred

For which I am getting Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.

Please help me on this !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.