1

I am trying to upload file via C# to sharepoint server 2013, I copy pasted the code from here.

I just added inside the constructor my credentials, like:

public SharePointClient(string webUri)
{
   Credentials=new NetworkCredential("myusername","mypassword");
}

And I'm calling the upload function from the main class:

var url="http://codey-sharepoint/sites/codey";
var sharepoint=new SharePointClient(url);
sharepoint.UploadFileAsChuncks(url,"D:\\test.txt");

But, I'm getting "BadRequest" exception at:

...
if (firstChunk)
{
   endpointUrl = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/startupload(uploadId=guid'{2}')", BaseAddress, uriAddress.LocalPath, uploadId);
    this.UploadData(endpointUrl, buffer);   //<----- here exception
    firstChunk = false;
}

Any suggestions?

  • Try to debug endpointUrl. What is the value? – Raf Jul 4 at 12:09
  • @Raf It looks like: http://codey-sharepoint/sites/codey/_api/web/getfilebyserverrelativeurl('/sites/codey')/startupload(uploadId=guid'933907ff-f426-42bc-aff4-a4cb8f79114a') – Codey Jul 4 at 12:14
  • @Codey, since getfilebyserverrelativeurl expects file server relative path, try to specify getfilebyserverrelativeurl('/sites/codey/path/filename') instead of getfilebyserverrelativeurl('/sites/codey') – Vadim Gremyachev Jul 4 at 15:41
  • @VadimGremyachev Thanks for replay, please see my replay to Lee_MSFT's answer – Codey Jul 7 at 10:46
0

Sample test code in my local SharePoint 2016.

Pay attention to add a empty file first, hardcode the library for testing purpose, update it accordingly

public void UploadFileAsChunks(string address, string fileName, int chunkSize = 2048)
        {
            var uriAddress = new Uri(address);
            var fi = new FileInfo(fileName);
            SetupRequest(HttpMethod.Post);

            var fileContent = System.IO.File.ReadAllBytes(fileName);

            //add a empty file first, hardcode the library for testing purpose, update it accordingly
            var emptyEndpointUrl = string.Format("{0}/_api/web/GetFolderByServerRelativeUrl('/mydoc')/Files/add(url='{2}',overwrite=true)", BaseAddress, uriAddress.LocalPath, fi.Name);
            this.UploadData(emptyEndpointUrl, new byte[] { });

            if (fi.Length <= chunkSize)
            {
                //this.UploadFile(address, fi.Name);                
                var UpdateEmptyEndpointUrl = string.Format("{0}/_api/web/GetFolderByServerRelativeUrl('/mydoc')/Files/add(url='{2}',overwrite=true)", BaseAddress, uriAddress.LocalPath, fi.Name);
                this.UploadData(emptyEndpointUrl, fileContent);
                return;
            }                                             

            var endpointUrl = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/savebinarystream", BaseAddress, uriAddress.LocalPath);

            var firstChunk = true;
            var uploadId = Guid.NewGuid();
            var offset = 0L;
            using (var inputStream = System.IO.File.OpenRead(fileName))
            {
                var buffer = new byte[chunkSize];
                int bytesRead;
                while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    if (firstChunk)
                    {
                        endpointUrl = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/startupload(uploadId=guid'{2}')", BaseAddress, uriAddress.LocalPath, uploadId);
                        this.UploadData(endpointUrl, buffer);
                        firstChunk = false;
                    }
                    else if (inputStream.Position == inputStream.Length)
                    {
                        endpointUrl = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/finishupload(uploadId=guid'{2}',fileOffset={3})", BaseAddress, uriAddress.LocalPath, uploadId, offset);
                        var finalBuffer = new byte[bytesRead];
                        Array.Copy(buffer, finalBuffer, finalBuffer.Length);
                        this.UploadData(endpointUrl, finalBuffer);
                    }
                    else
                    {
                        endpointUrl = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/continueupload(uploadId=guid'{2}',fileOffset={3})", BaseAddress, uriAddress.LocalPath, uploadId, offset);
                        this.UploadData(endpointUrl, buffer);
                    }
                    offset += bytesRead;
                }
            }
        }

Call the lib.

var url = "http://sp:12001";
            var sharepoint = new SharePointClient(url);
            sharepoint.UploadFileAsChunks("http://sp:12001/Mydoc/screen.gif", "C:\\Lee\\screen.gif");
            Console.WriteLine("complete");
  • It creates an empty file, but when It goes into "first chunck" uploaddata mehod with this url http://codey-sharepoint/sites/codey/_api/web/GetFileByServerRelativeUrl('/sites/codey/Shared Documents/300MB.txt')/startupload(uploadId=guid'26903998-f2ce-4f83-91f1-0e3541517a8c') I'm getting 404 Not Found – Codey Jul 7 at 10:44
  • Does 300MB.txt created? if created, should not raise 404 based on my testing. – Lee_MSFT Jul 8 at 6:55
  • I suspect that getfilebyserverrelativeurl doesn't work for sharepoint 2013 @Lee_MSFT – Codey Jul 8 at 7:08
  • Missing the tag, SharePoint 2013, suppose you're using 2016, startupload is available from SharePoint 2016. – Lee_MSFT Jul 8 at 7:44
  • so there is no way to upload large files (~1GB) to sharepoit server 2013 ? I have tried with SaveBinaryDirect and all other options in the link, but nothing worked for files>300 MB. evan taht I succeded to upload manually – Codey Jul 8 at 7:48

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.