2

I have a project to upload files to Sharepoint using REST API ( POST). I am using vb.net and RestSharp. Code is below. this code is working. I am able to do with the small Text file only and the file is getting properly upload into Sharepoint. But when it comes to another file format ( XLS, XLSX, PDF, JOG, Doc ..etc) with a file size greater than 1 MB file is getting uploaded with it, not in a readable format. I tried to upload a ZIP file for 55 MB it is getting Uploaded. But When I download the same uploaded file I am not able to read it. Can anyone please help me, to get this fixed? ** I would need a solution to upload any file format for any size ( In my project I will not use any file more than 500 MB for upload. )

Below is the code is tried.

Public Sub UploadFile2()

        Dim FullfileName As String = "E:\Desktop\try\FileToUpload.xlsx"
        Dim NewFileName As String = "NewFile.xlsx"

        Dim uploadFileUrl As String = SiteUrl + "_api/web/lists/getByTitle('Documents')/RootFolder/Files/Add(url='" + NewFileName + "', overwrite=true)"

        Dim client = New RestClient(uploadFileUrl) With {
            .Timeout = -1
        }
        Dim UploadRequest = New RestRequest(Method.POST)

        UploadRequest.AddHeader("Accept", "application/json;odata=verbose")
        UploadRequest.AddHeader("X-RequestDigest", DigestValue)
        UploadRequest.AddHeader("Authorization", "Bearer " + AccessToken)
        UploadRequest.AddHeader("Content-Type", "multipart/form-data")
        UploadRequest.AlwaysMultipartFormData = True
        UploadRequest.AddFile("FileName", FullfileName)
        UploadResponse = client.Execute(UploadRequest)

    End Sub
1
  • Hi Rajnish, do you remember how you have managed to fix this? I have the same issue with images (PNG and JPEG). Files are being uploaded, the size is correct, but these images cannot be previewed or opened in SharePoint Online and cannot be opened after downloading them as well. I'm trying to test this using Kotlin Spring Boot app and simply sending the requests from Postman – result is the same. P.S. I pass the content length in header as proposed in the answer below.
    – alexkov
    Aug 17, 2021 at 11:38

2 Answers 2

0

Looks like you need to pass the Content-Length header.

0

For anyone interested, this worked for me:

Public Sub UploadFile2()

Dim FullfileName As String = "E:\Desktop\try\FileToUpload.xlsx"
Dim NewFileName As String = "NewFile.xlsx"

Dim uploadFileUrl As String = SiteUrl + "_api/web/lists/getByTitle('Documents')/RootFolder/Files/Add(url='" + NewFileName + "', overwrite=true)"

Dim client = New RestClient(uploadFileUrl) With {
    .Timeout = -1
}
Dim UploadRequest = New RestRequest(Method.POST)

UploadRequest.AddHeader("Accept", "application/json;odata=verbose")
UploadRequest.AddHeader("X-RequestDigest", DigestValue)
UploadRequest.AddHeader("Authorization", "Bearer " + AccessToken)
UploadRequest.AddHeader("Content-Type", "application/octet-stream")
UploadRequest.AddParameter("application/octet-stream", File.ReadAllBytes(FullfileName), ParameterType.RequestBody)
UploadResponse = client.Execute(UploadRequest)
End Sub
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 20, 2022 at 2:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.