0

I am calling API https://do.convertapi.com/Pdf2PowerPoint

Their website for the API details is https://www.convertapi.com/

To upload the file in their C# documentation they have used client.UploadFile() function which expects file name parameter from physical location. in my case i have bytes of PDF file coming on the fly without storing that to physical location and I want to upload that bytes instead. I am using client.UploadData() function which expects byte array and I have provide that. but their API is throwing error and is asking to provide File name which is must.

I think Developers of API would only be able to answer. But if you guys have any idea if I am doing any mistake to upload file. please suggest your workaround.

Please find my code below as requested

            var client = new WebClient();
            var data = new NameValueCollection();
            data.Add("OutputFileName", "TestOutput.pptx"); //Optional
            data.Add("File", "Revised.pdf");
            data.Add("ApiKey", "484700111"); //API Key must be set if you purchased membership with credits. Please login to your control panel to find out your API Key http://www.convertapi.com/prices

            try
            {
                client.QueryString.Add(data);
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                //I am using ReadAllBytes Approach for now as in my practical scenario I am going to get bytes instead of sending file from Physical location
                byte[] Arr = File.ReadAllBytes(@"D:\PPTTest\Level I and II Revised.pdf");
                // Error here : File Parameter can not be null
                var response = client.UploadData("https://do.convertapi.com/Pdf2PowerPoint", Arr);
                var responseHeaders = client.ResponseHeaders;
                var path = Path.Combine(@"D:\PPTTest\", responseHeaders["OutputFileName"]);
                File.WriteAllBytes(path, response);
                //Console.WriteLine("The conversion was successful! The word file {0} converted to PDF and saved at {1}", fileToConvert, path);
            }
            catch (WebException e)
            {
                Console.WriteLine("Exception Message :" + e.Message);
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                }

            }

Thanks, Hira

1 Answers1

1

Code taken from this post. You have to upload file with multipart/form-data request like this:

    HttpClient httpClient = new HttpClient();
    MultipartFormDataContent form = new MultipartFormDataContent();

    form.Add(new StringContent(username), "username");
    form.Add(new StringContent(useremail), "email");
    form.Add(new StringContent(password), "password");            
    form.Add(new ByteArrayContent(imagebytearraystring, 0, imagebytearraystring.Count()), "profile_pic", "hello1.jpg");
    HttpResponseMessage response = await httpClient.PostAsync("PostUrl", form);
Community
  • 1
  • 1
Jonas
  • 4,683
  • 4
  • 45
  • 81