Having problems uploading PDF file via REST API using C#

I am currently writing a C# program that uses the Sharepoint REST API to manage uploading and downloading of files.

Currently can retrieve files stored in folders in a subsite without problems, but I am having issues with uploading files to the same folders.

I use “/_api/web/getfolderbyserverrelativeurl(‘/Folder1/Folder2/File)$value” to retrieve a file. This works fine.

I can also get the folder itself via “_api/web/getfolderbyserverrelativeurl(‘/Folder1/Folder2′)
I am thinking I can use the same URI structure to add a file to that folder by adding “/files/add(url=’filename’, overwrite=true)” to the end of the above URI.
Set the method to “POST”, Accept to “application/json; odata=verbose”, ConentLength to length of file being sent. Also added Authorization and X-RequestDigest headers to the webrequest.

When the webrequest is sent to the server I get the following errors back (after about 30secs).

  • HttpRequestException: An error occurred while sending the request
  • IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host
  • SocketException: An existing connection was forcibly closed by the remote host

My question is… Has anyone else seen these sort of errors before and am I approaching this is the correct way.

Also related to this, once I get this file uploaded how do I add metadata to it? Or does the meta data get added to the file as it is uploaded?

I seem to have sorted the initial errors with the post request. Did a rewrite with the way I was sending the request. Now uses the HttpWebRequest object to do the work. But now I am getting a “400 Bad Request” error. This is the request URL that I am using https://server/_api/web/GetFolderByServerRelativeUrl('/Folder1/Folder2/Folder3/Folder4')/Files/add(url='FileName',overwrite=False) Have I missed something in constructing the request?
Further to all this, I have a feeling that the way I am uploading the pdf file is incorrect.
Here is my code that I am using to send.

HttpWebRequest restRqst = (HttpWebRequest)HttpWebRequest.Create(endpointUrl);
restRqst.UseDefaultCredentials = false;
restRqst.Credentials = auth.GetCredentials(_userName, _password);
restRqst.Method = "POST";
restRqst.Accept = "application/json; odata=verbose";
restRqst.Headers.Add("Authorization", "Bearer " + auth.GetAccessToken(_siteUri, _userName, _password));
restRqst.Headers.Add("X-RequestDigest", auth.GetFormDigestValue(_sharePointSiteURL, auth.GetCredentials(_userName, _password), auth.GetAccessToken(_siteUri, _userName, _password)));
restRqst.Timeout = 1000000;

var b = stream.ToArray();
restRqst.ContentLength = b.Length;
try
    {
     using (Stream postStream = restRqst.GetRequestStream())
     {
      postStream.Write(b,0, b.Length);
      postStream.Close();
     }
    }
catch(Exception ex)
    {
      Console.WriteLine(ex.Message);
    }
try
    {
     WebResponse wresp = restRqst.GetResponse();
     using (System.IO.StreamReader sr = new System.IO.StreamReader(wresp.GetResponseStream()))
      {
       result = sr.ReadToEnd();
       status = true;
       return Task.CompletedTask;
      }
    }
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    throw;
}

Any thoughts or comments are very much appreciated.

performance – Why is my external hard drive uploading so slowly on USB 3.0?

I have approximately 1.5 TB of data which I need to back-up on an external hard drive. I am using a brand new 2 TB Seagate OneTouch HDD portable storage device with a USB 3.0 cable.

Currently, the expected transfer time is over 45 hours and climbing.

Maybe my math is wrong but 1.5 TB at 600 MBps using a USB 3.0 cable should only take about 45 minutes, right? Even if I was just using a USB 2.0 cable with 60 MBps, it still shouldn’t take much more than 7 hours.

What’s the issue here and is there some solution?

Android APP FTP uploading file implementation in Java

Android APP FTP uploading file implementation in Java – Code Review Stack Exchange

8 – Uploading a large file from the phone doesn’t work

I am using Drupal 8.9.13 and Webform 8.x-5.19. I created a form with only one field, File. (For testing purposes, I put also the file upload field). Drag and drop is also enabled.

I increased the file upload size and the maximum post size to 20 MB.

Uploading files works fine in desktop and laptop, but from a phone/mobile device it doesn’t work. The background process (AJAX) of file uploading goes into a endless loop.

Any solution/suggestion is welcome.

Thanks in advance.

How do I link my website and YouTube channel? I know it involves uploading a file on my web server but unable to find the option in YouTube settings


Your privacy


By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.




Excel file corrupted after uploading to SharePoint with VB.NET

Excel file corrupted after uploading (.xlsx file) to SharePoint with VB.NET, I got that error message while opening excel file after uploaded to SharePoint “Excel cannot open the file ‘Input.xlsx’ because the file format or file extension is not valid. Verify that the file has not been currented and that the file extension matches the format of the file”. Can you please share the VB.NET code to upload .xlsx file SharePoint.

Dim client As HttpClient = New HttpClient()
client.BaseAddress = New Uri("https://****.sharepoint.com/sites/ChinaBot/_api/web/GetFolderByServerRelativeUrl('FilderName')/Files/add(url='File.xlsx',overwrite=true)")
client.DefaultRequestHeaders.Accept.Clear()
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
client.DefaultRequestHeaders.Add("binaryStringRequestBody", "true")
client.DefaultRequestHeaders.Add("authorization", "Bearer Access Token")
Dim request As MultipartFormDataContent = New MultipartFormDataContent()
request.Add(New StringContent("Bad"), "tags")
request.Add(New StringContent("true"), "displayreferencetext")
request.Add(New StringContent("0.63"), "similaritythreshold")
request.Add(New StreamContent(File.OpenRead("C:\SharePoint\P2015548 DT PMT.xlsx")), "file", (New FileInfo("C:\SharePoint\P2015548 DT PMT.xlsx").Name))
Dim response As HttpResponseMessage = client.PostAsync("", request).Result
Dim strm As Stream = response.Content.ReadAsStreamAsync.Result
Dim doc As Byte()
Dim ms As MemoryStream = New MemoryStream()
strm.CopyTo(ms)
doc = ms.ToArray()
File.WriteAllBytes("C:\RPA Files\rez.docx", doc)

samsung – Google Drive stuck uploading files

Three days ago I was uploading 6 files to a Google Drive shared folder. It’s not the first time I do that, but surely this time was different because not only the files weren’t uploaded but also since then I have this notification

enter image description here

To fix it, I tried to

  1. Click cancel.
  2. Restart the phone.
  3. Force stop the app in settings.

yet none of these options worked.

itunes – Is there any way to transfer mp3 files from PC to iPhone without uploading them to the cloud first?

When I do a search for how to transfer mp3 files from PC to iPhone I invariably come across answers that mention iTunes. E.g:

How to copy MP3 files from my PC to my iphone?

My guess is that what’s happening here is that the files are first getting uploaded to the cloud and from there they are downloaded to iPhone. Is that correct? If not, then why does one have to use iTunes?

I have very large amount of data. Is there any way to transfer files directly from PC to iPhone via USB without them being uploaded to the cloud first? The files could even be confidential and so I don’t want to upload them to the cloud.

DreamProxies - Cheapest USA Elite Private Proxies 100 Private Proxies 200 Private Proxies 400 Private Proxies 1000 Private Proxies 2000 Private Proxies ExtraProxies.com - Buy Cheap Private Proxies Buy 50 Private Proxies Buy 100 Private Proxies Buy 200 Private Proxies Buy 500 Private Proxies Buy 1000 Private Proxies Buy 2000 Private Proxies ProxiesLive Proxies-free.com New Proxy Lists Every Day Proxies123