locked
Using convertapi in Windows Store App

    Question

  • Im trying out the Convertapi in a Windows store App project, and i want to send a .docx file and get a pdf file in return, im trying to do a post but im not sure how its done, this is what i have so far, but its not working.

        private async Task GeneratePdfContract(string path) {
    try {
        var data = new List < KeyValuePair < string, string >> {
                new KeyValuePair < string, string > ("Api", "5"),
                    new KeyValuePair < string, string > ("ApiKey", "413595149"),
                    new KeyValuePair < string, string > ("File", "" + stream2),
    
            };
    
        await PostKeyValueData(data);
    
    } catch (Exception e) {
    
        Debug.WriteLine(e.Message);
    }

    }

    private async Task PostKeyValueData(List < KeyValuePair < string, string >> values) {
    var httpClient = new HttpClient();
    var response = await httpClient.PostAsync("http://do.convertapi.com/Word2Pdf", new FormUrlEncodedContent(values));
    var responseString = await response.Content.ReadAsStringAsync();

    }

    How should i do my post to send a .docx file and get a .pdf file in return?

    Tuesday, October 21, 2014 3:34 PM

Answers

  • Hi Thought2,

    That's kind of expected behavior.

    Please take a look at the documentation: File access and permissions, I think you did something wrong with the file path, "C:" is not allow in Windows Store App.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 23, 2014 1:00 AM
    Moderator

All replies

  • Hi Thought2,

    Try to validate if your parameters are passed correctly, as I test online with your ApiKey, something wrong there.

    Double check if you have all things correct for instance confirm if your file upload is a binary and etc.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, October 22, 2014 6:44 AM
    Moderator
  • im trying something diferent now

    private async Task GeneratePdfContract(string path)
            {
                try
                {
                    using (var client = new System.Net.Http.HttpClient())
                    {
                        using (var multipartFormDataContent = new MultipartFormDataContent())
                        {
                            var values = new[]
            {
                new KeyValuePair<string, string>("ApiKey", "419595049")
            };
    
                            foreach (var keyValuePair in values)
                            {
                                multipartFormDataContent.Add(new StringContent(keyValuePair.Value), String.Format("\"{0}\"", keyValuePair.Key));
                            }
    
                            StorageFolder currentFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(Constants.DataDirectory);
    
                            StorageFile outputFile = await currentFolder.GetFileAsync(Constants.B2bWordContractFile);
    
                            byte[] fileBytes = await outputFile.ToBytes();
    
    
                            //multipartFormDataContent.Add(new ByteArrayContent(FileIO.ReadBufferAsync(@"C:\test.docx")), '"' + "File" + '"', '"' + "test.docx" + '"');
    
                            multipartFormDataContent.Add(new ByteArrayContent(fileBytes));
    
                            const string requestUri = "http://do.convertapi.com/word2pdf";
    
                            var response = await client.PostAsync(requestUri, multipartFormDataContent);
                            if (response.IsSuccessStatusCode)
                            {
                                var responseHeaders = response.Headers;
                                var paths = responseHeaders.GetValues("OutputFileName").First();
                                var path2 = Path.Combine(@"C:\", paths);
    
    
    
                                StorageFile sampleFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"C:\Users\Thought\AppData\Local\Packages\xxxx_apk0zz032bzya\LocalState\Data\");
                                await FileIO.WriteBytesAsync(sampleFile, await response.Content.ReadAsByteArrayAsync());
    
    
                            }
                            else
                            {
                                Debug.WriteLine("Status Code : {0}", response.StatusCode);
                                Debug.WriteLine("Status Description : {0}", response.ReasonPhrase);
                            }
    
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
    
            }


    But i get something like this in return in my response variable

    and this on output

    Status Code : BadRequest
    Status Description : The 'File' parameter cannot be null. Please set value.


    • Edited by Thought2 Wednesday, October 22, 2014 9:57 PM
    Wednesday, October 22, 2014 9:50 PM
  • Hi Thought2,

    That's kind of expected behavior.

    Please take a look at the documentation: File access and permissions, I think you did something wrong with the file path, "C:" is not allow in Windows Store App.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 23, 2014 1:00 AM
    Moderator