'Upload image to java rest api from .net http client

I am student and on a assignment i was trying to send image from my blazor page to java rest api . As for the api it works perfectly as i tried it with postman but I can not figure it out how to send with blazor -- http client.

// After 2 days :D I figured it out

// Now can anyone help me how to send this received file from java to C# .net using gRPC That would be helpful

This is my java controller

 @PostMapping("/upload-Image")
public String uploadImage(@RequestBody()MultipartFile file) throws IOException {

    System.out.println(file.getOriginalFilename());
    System.out.println(file.getName());
    System.out.println(file.getContentType());
    System.out.println(file.getSize());


    String pathdirectory = new ClassPathResource("static/image").getFile().getAbsolutePath();
    Files.copy(file.getInputStream(), Paths.get(pathdirectory+file.getOriginalFilename()), StandardCopyOption.REPLACE_EXISTING);

   // return file;
    return "Image is successfully uploaded";

}

My index.razor file code

<InputFile OnChange="@OnInputFileChange"/>
@code

{ Client client = new Client();

private async void OnInputFileChange(InputFileChangeEventArgs e)
{
    Console.Write("Called function");


    var file = e.File;

   await client.AddAsync(file);
}

}

-----Inside My Client.cs class---

public async Task<string> AddAsync(IBrowserFile file)
{

                var form = new MultipartFormDataContent();
            using (var fs = file.OpenReadStream())
            {
                using (var streamContent = new StreamContent(fs))
                {
                    using (var fileContent = new ByteArrayContent(await streamContent.ReadAsByteArrayAsync()))
                    {
                        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");

                        form.Add(fileContent, "file", file.Name);
using var httpClient1 = new HttpClient();

 HttpResponseMessage responseForImage =
                await httpClient1.PostAsync($"http://localhost:8080/upload-Image", form);

                        

                    }
                }
            }

}

// Now no error And this is the error

ERROR 5686 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.multipart.MultipartFile.getOriginalFilename()" because "file" is null] 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source