https://do.convertapi.com/Pdf2PowerPoint 不提供物理文件位置

https://do.convertapi.com/Pdf2PowerPoint without providing physical file location

我正在打电话API https://do.convertapi.com/Pdf2PowerPoint

API 详细信息的网站是 https://www.convertapi.com/

为了在他们的 C# 文档中上传文件,他们使用了 client.UploadFile() 函数,该函数需要来自物理位置的文件名参数。在我的例子中,我有字节的 PDF 文件正在运行,但没有将其存储到物理位置,我想上传这些字节。我正在使用需要字节数组的 client.UploadData() 函数,我已经提供了它。但是他们的 API 抛出错误并要求提供必须的文件名。

我想 API 的开发者只能回答。但是如果你们知道我在上传文件时是否犯了任何错误。请提出您的解决方法。

请按照要求在下面找到我的代码

            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);
                }

            }

谢谢, 希拉

Code taken from this post。您必须使用 multipart/form-data 请求上传文件,如下所示:

    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);