1

I have a task which I need to upload a pdf converted to base64 string to Http post. I tried it using refit before but I failed to upload it. Now, I am trying a difference approach.

I used the web request POST method, First I converted the PDF to base64 but on the part that I call the stream.write method, I'm having an error since the file is already converted to base64 string and the stream.write paramater is byte array.

Also, how can I add these to the body using web request?

"mime": "application/pdf",

"data": "base64-data="

 string pdfBase64;

 const string pdfFileName = "file.pdf";
    
                    using (var pdfStream = await FileSystem.OpenAppPackageFileAsync(pdfFileName))
                    {
                        using (var pdfReader = new StreamReader(pdfStream))
                        {
                            var fileContents = await pdfReader.ReadToEndAsync();
                            pdfBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContents));
                        }
                    }
    
                    WebRequest request = WebRequest.Create("https://goodmorning-axa-dev.azure-api.net/upload");
                    request.Method = "POST";
                    request.ContentLength = pdfBase64.Length;
                    request.ContentType = "application/json";             
                    request.Headers.Add("x-axa-api-key", apiKey);
    
                    Stream stream = request.GetRequestStream();
                    stream.Write(pdfBase64, 0, pdfBase64.Length);
                    stream.Close();
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    stream = response.GetResponseStream();
    
                    StreamReader sr = new StreamReader(stream);
                    Toast.MakeText(this, sr.ReadToEnd(), ToastLength.Short).Show();
                    
                    sr.Close();
                    stream.Close();
error404
  • 13
  • 7
  • https://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp – Jason Oct 22 '20 at 17:05
  • @Jason the problem is that, the requirement wants a pdf converted to base64 string. I already did the conversion but the stream.write method needs a byte array parameter. do I have to convert it again to byte array after converting to base64 string? – error404 Oct 22 '20 at 17:29
  • A glance at your "requirements" image shows that is expecting a JSON object, so I'm really not sure what you're doing with the stream. There are many, many questions already answered about posting JSON – Jason Oct 22 '20 at 17:33
  • @Jason because I also need to send a pdf file that is converted to base64 – error404 Oct 22 '20 at 22:02
  • @Jason I’m still new to this so I’m kinda lost. still doing some research – error404 Oct 22 '20 at 22:04
  • 1) create an class to model the data required by the API, 2) serialize it to json, 3) POST using one of the many, many, many existing tutorials on posting json – Jason Oct 22 '20 at 22:10
  • @error404 From [Stream.Write Method](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.write?view=netcore-3.1), you can see the first parameter is Byte[], then pdfBase64 is string, I gues that you want to pass `Encoding.UTF8.GetBytes(fileContents)` – Cherry Bu - MSFT Oct 23 '20 at 03:34

0 Answers0