Exemple #1
0
        public static async Task <FileInfo> GetConvertedResponseBySource(string filePath, string destinationFilePath)
        {
            ConvertApi         convertApi = new ConvertApi("<your secret key>"); // get secret from https://www.convertapi.com/a via register
            ConvertApiResponse result     = await convertApi.ConvertAsync("xlsx", "pdf", new[]
            {
                new ConvertApiFileParam(filePath),
            });

            // save to file
            var fileInfo = await result.SaveFileAsync(destinationFilePath);

            return(fileInfo);
        }
        private async Task ConvertFiles(string fromFormat, string toFormat, string conversionGuid)
        {
            string filesToConvertPath = Path.Combine(_environment.ContentRootPath, $"wwwroot\\Files\\{conversionGuid}");
            string convertedFilesPath = Path.Combine(filesToConvertPath, "Converted");

            string[] filePaths = Directory.GetFiles(filesToConvertPath);

            foreach (string filePath in filePaths)
            {
                ConvertApiResponse convertResponse = await convertApi.ConvertAsync(fromFormat, toFormat,
                                                                                   new ConvertApiFileParam("File", filePath),
                                                                                   new ConvertApiParam("FileName", Path.GetFileNameWithoutExtension(filePath))
                                                                                   );

                await convertResponse.SaveFilesAsync(convertedFilesPath);
            }
        }
Exemple #3
0
        // POST: api/Upload
        public async Task <IHttpActionResult> Post()
        {
            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0) //check if request contains file
            {
                //get file from the request
                foreach (string file_Name in httpRequest.Files.Keys)
                {
                    var file = httpRequest.Files[file_Name];
                    if (file == null || file.ContentLength == 0) //check if file is bad
                    {
                        return(Content(HttpStatusCode.BadRequest, new { message = "Length of received file is 0. Either you submitted an empty file, or no file was selected before submission." }));
                    }

                    //file paths to store the uploaded and converted files in
                    var file_Path           = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "document.docx");
                    var converted_file_Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "document.pdf");

                    file.SaveAs(file_Path);

                    try
                    {
                        //ConvertAPI client code to convert docx to pdf

                        ConvertApiResponse result = await convertApi.ConvertAsync("docx", "pdf", new[] {
                            new ConvertApiFileParam(File.OpenRead(file_Path), "document.docx")
                        });

                        File.Delete(converted_file_Path);
                        // save to file
                        var fileInfo = await result.SaveFileAsync(converted_file_Path);

                        return(Ok());
                    }
                    catch (Exception e)
                    {
                        return(Content(HttpStatusCode.BadRequest, new { message = "An error occurred while attempting to convert the file to PDF. The error is: " + e.Message }));
                    }
                }
            }

            return(Content(HttpStatusCode.BadRequest, new { message = "No file was found in the request." }));
        }
        public async Task <IActionResult> ConverterMoeda(double amount, string from, string to)
        {
            HttpClient client = MyConvertHttpClient.Client;
            string     path   = "/api/CurrencyConvert";

            ConvertApiRequest req  = new ConvertApiRequest(amount, from, to);
            string            json = JsonConvert.SerializeObject(req);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, path);

            request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                return(Redirect("/"));
            }
            string json_r = await response.Content.ReadAsStringAsync();

            ConvertApiResponse crMoeda = JsonConvert.DeserializeObject <ConvertApiResponse>(json_r);

            return(View("ConverterMoedaResultado", crMoeda));
        }
Exemple #5
0
 public ConvertApiFileParam(string name, ConvertApiResponse response) : base(name)
 {
     Value = response.Files.Select(s => s.Url.ToString()).ToArray();
 }
Exemple #6
0
 public ConvertApiFileParam(ConvertApiResponse response) : this("File", response)
 {
 }
 public static async Task <List <FileInfo> > SaveFilesAsync(this ConvertApiResponse response, string outputDirectory)
 {
     return(await response.Files.SaveFilesAsync(outputDirectory));
 }
 public static async Task <FileInfo> SaveFileAsync(this ConvertApiResponse response, string fileName)
 {
     return(await response.Files[0].SaveFileAsync(fileName));
 }
 /// <summary>
 /// Return the count of converted files
 /// </summary>
 /// <returns>Files converted</returns>
 public static int FileCount(this ConvertApiResponse response)
 {
     return(response.Files.Length);
 }
Exemple #10
0
 public static Task <FileInfo> AsFileAsync(this ConvertApiResponse response, int fileIndex, string fileName)
 {
     return(AsFileAsync(response.Files[fileIndex].Url, fileName));
 }
Exemple #11
0
 private static Stream AsFilesStream(this ConvertApiResponse response, int fileIndex)
 {
     return(AsStreamAsync(response.Files[fileIndex].Url).Result);
 }
Exemple #12
0
 private static IEnumerable <Task <Stream> > AsFilesStreamAsync(this ConvertApiResponse response)
 {
     return(response.Files.Select(s => AsStreamAsync(s.Url)));
 }
Exemple #13
0
 public static FileInfo SaveFile(this ConvertApiResponse response, string fileName)
 {
     return(response.Files[0].AsFileAsync(fileName).Result);
 }
Exemple #14
0
 public static FileInfo[] SaveFiles(this ConvertApiResponse response, string directory)
 {
     return(response.Files.Select(file => AsFileAsync(file.Url, Path.Combine(directory, Path.GetFileName(file.FileName)))).Select(task => task.Result).ToArray());
 }
Exemple #15
0
 public ConvertApiParam(string name, ConvertApiResponse convertApiResponse) : this(name, convertApiResponse.Files.Select(s => s.Url.ToString()).ToArray())
 {
 }