ConvertAPI - 在 C# 中将 PDF 转换为 PPT
ConvertAPI - PDF to PPT in C#
我们正在升级到 ConvertAPI 指定的新逻辑。但是我们使用的框架是 4 因为没有找到 System.Net.Http(HttpResponseMessage
, HttpClient
,MultipartFormDataContent
,StreamContent
,StringContent
) 类 .甚至添加了 System.Net.Http
参考。但仍然存在相同的错误。
有人可以为此建议一些解决方法吗?如何在 C# 中为 .Net Framework 4 使用 ConvertApi?
只需使用 WebClient 而不是 HttpClient 即可快速上传文件。对于下面的 运行 演示,您需要替换文件路径并在端点 url 中设置您的秘密而不是 xxxxx。我们还应该设置 accept:application/octet-stream
header 来获取响应作为数据流而不是 json.
class Program
{
static void Main(string[] args)
{
const string fileToConvert = @"C:\Projects\_temp\test1.docx";
const string fileToSave = @"C:\Projects\_temp\test1.pdf";
try
{
using (var client = new WebClient())
{
client.Headers.Add("accept", "application/octet-stream");
var resultFile = client.UploadFile(new Uri("https://v2.convertapi.com/docx/to/pdf?Secret=xxxxx"), fileToConvert);
File.WriteAllBytes(fileToSave, resultFile );
Console.WriteLine("File converted successfully");
}
}
catch (WebException e)
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
Console.WriteLine("Body : {0}", new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
}
Console.ReadLine();
}
}
我们正在升级到 ConvertAPI 指定的新逻辑。但是我们使用的框架是 4 因为没有找到 System.Net.Http(HttpResponseMessage
, HttpClient
,MultipartFormDataContent
,StreamContent
,StringContent
) 类 .甚至添加了 System.Net.Http
参考。但仍然存在相同的错误。
有人可以为此建议一些解决方法吗?如何在 C# 中为 .Net Framework 4 使用 ConvertApi?
只需使用 WebClient 而不是 HttpClient 即可快速上传文件。对于下面的 运行 演示,您需要替换文件路径并在端点 url 中设置您的秘密而不是 xxxxx。我们还应该设置 accept:application/octet-stream
header 来获取响应作为数据流而不是 json.
class Program
{
static void Main(string[] args)
{
const string fileToConvert = @"C:\Projects\_temp\test1.docx";
const string fileToSave = @"C:\Projects\_temp\test1.pdf";
try
{
using (var client = new WebClient())
{
client.Headers.Add("accept", "application/octet-stream");
var resultFile = client.UploadFile(new Uri("https://v2.convertapi.com/docx/to/pdf?Secret=xxxxx"), fileToConvert);
File.WriteAllBytes(fileToSave, resultFile );
Console.WriteLine("File converted successfully");
}
}
catch (WebException e)
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
Console.WriteLine("Body : {0}", new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
}
Console.ReadLine();
}
}