0

我正在调用 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);
                }

            }

谢谢,希拉

4

1 回答 1

1

取自这篇文章的代码。您必须使用 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);
于 2017-03-09T15:19:40.833 回答