AnsweredAssumed Answered

Upload document in alfresco using rest api via C# console application.

Question asked by mohitrth26 on Jun 21, 2019

Hello,

 

Need your help..

 

I need to upload document using rest api via C# console application.

 

I tried below code but throwing 500 error.

 

URL: localhost:8080/alfresco/service/api/upload?alf_ticket=TICKET_ee4742c300cb2a0475ccaaea45d9c477bad8d536

 

public bool PutObject(String URL)
        {
            try
            {
                FileStream fs = newFileStream("C:\\getDeliverdDeal.html",FileMode.OpenOrCreate,FileAccess.ReadWrite);
                string des = "workspace://SpacesStore/2114c3e4-fe78-443b-b033-3f467504deb3";
                var values = "{\"filedata\":\""+fs+"\",\"destination\":\""+des+"\"}";
//                var fd = new FormData();
                Console.WriteLine(values);
                //var values = "{\"Request\":{\""+ FileIds + "\"}}";
 
                var bytes = Encoding.ASCII.GetBytes(values);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
              //  request.ContentType = "undifined";
 
                using (var requestStream = request.GetRequestStream())
                {
                   requestStream.Write(bytes, 0, bytes.Length);
 
                    try
                    {
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        if (response.StatusCode == HttpStatusCode.OK)
                            return true;
 
                        else
                            return false;
                    }
                    catch (WebException ex)
                    {
                        Console.WriteLine(ex.Message);
                        if (ex.Status == WebExceptionStatus.ProtocolError)
                        {
                            return false;
                        }
                        else return false;
                    }
                }
 
 
 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
 
        }

Outcomes