Question to upload file by using Restsharp C# to owncloud server? Thanks

2,443 views
Skip to first unread message

Carvin Chen

unread,
May 25, 2017, 3:33:42 PM5/25/17
to RestSharp
Hello, everyone!

I am learning the cloud computing and trying to write a little programm that uploads a local file from PC to owncloud server (Ubuntu 16.04). 

I am using visual studio + open source lib restsharp in c# as development environment and have tried many different functions since a week. But it dosent work.., i didnt see any files have been uploaded onto the owncloud server (path), also no error informations...so it seems like unsuccessful..

The big part of my code is below and i really hope, someone could help me to understand, why my code dosent work and maybe some example that work well? 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using RestSharp;
using RestSharp.Extensions.MonoHttp;
using RestSharp.Authenticators;
using RestSharp.Extensions; //For SaveAs function, example: client.DownloadData(request).SaveAs(path);
using System.IO;
using System.Net;
using System.Globalization;

namespace myOwncloudProj
{
    class Program
    {
        static void Main(string[] args)
        {

            try {

                var client = new RestClient();
                string cloudUsr = "user";
                string cloudPw = "pw";

                string pathToSave = @"D:\path\to\files\";

                client.BaseUrl = new Uri("http://192.168.xxx.xxx/owncloud/remote.php/webdav/");
                client.Authenticator = new HttpBasicAuthenticator(cloudUsr, cloudPw);

                UploadToCloud(client, "tmp.docx", pathToSave);

DownloadToPC(client, "tmp.docx", pathToSave);

            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            // Verzeichnis existiert nicht
            catch (DirectoryNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (WebException webExcp)
            {
                // If you reach this point, an exception has been caught.
                Console.WriteLine("A WebException has been caught.");
                // Write out the WebException message.
                Console.WriteLine(webExcp.ToString());
                // Get the WebException status code.
                WebExceptionStatus status = webExcp.Status;
                // If status is WebExceptionStatus.ProtocolError, 
                //   there has been a protocol error and a WebResponse 
                //   should exist. Display the protocol error.
                if (status == WebExceptionStatus.ProtocolError)
                {
                    Console.Write("The server returned protocol error ");
                    // Get HttpWebResponse so that you can check the HTTP status code.
                    HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
                    Console.WriteLine((int)httpResponse.StatusCode + " - "
                       + httpResponse.StatusCode);
                }
            }
            catch (Exception e_cld)
            {
                Console.WriteLine("An error occurred: '{0}'", e_cld);
            }
            Console.ReadLine();
        }

        //Upload file to owncloud server
        public static void UploadToCloud(RestClient ocClient, string fileName, string ocLocal)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            //Check if the file exists on PC
            string localFile = ocLocal + fileName;
            if (File.Exists(localFile) == true)
            {
                Console.WriteLine("local file exits on pc. \n");

                //Check if the file exits on server *********

                //Or upload it directlly
                var requestUpLoad = new RestRequest("/", Method.POST);
                requestUpLoad.AddHeader("Content-Type", "application/octet-stream");

                byte[] dataToUpload = File.ReadAllBytes(localFile);
                requestUpLoad.AddFile("tmpFile", dataToUpload, "tmp.docx");
                Console.WriteLine("Size of file to upload: " + dataToUpload.Length.ToString() + "\n");

                //ocClient.Execute(requestUpLoad);

                var cloudResponse = ocClient.Execute(requestUpLoad);
                Console.WriteLine("Resp: " + cloudResponse.ContentType + "\n");
                Console.WriteLine("Content: " + cloudResponse.Content + "\n");

                Console.WriteLine("Upload has been finished. \n");

            }
            else
            {
                Console.WriteLine("\n" + fileName + " is not found on pc. \n");
            }
        }

    }
}


 



Thank your for your help and i am grad to read your answer!

Cheers!

David

Chris Ondrovic

unread,
Aug 20, 2018, 2:37:30 PM8/20/18
to RestSharp
Hey David,

Trying replace this line

requestUpLoad.AddFile("tmpFile", dataToUpload, "tmp.docx");
Enter code here...

with the following

requestUpLoad.AddParameter("application/octect-stream", dataToUpload, ParameterType.RequestBody);

I recent ran into a similar problem
Reply all
Reply to author
Forward
0 new messages