Trying to upload a file using SfUploader through the ValueChange event

Hello,

So what i'm trying to achieve right now is uploading a file to an REST API with some custom headers. To achieve this instead of using the standard SaveUrl and RemoveUrl i just use the ValueChanged event to catch whenever a new file is added and upload that manually through my own http client with the custom headers.

However i don't seem to get this to work. Here is the code i have:

As seen in the code i basically use the same idea as documented for the regular InputFile component: https://docs.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-6.0&pivots=server#upload-files-to-a-server

For the InputFile component it does work, however now when i build the same MultipartFormDataContent using the Stream provided by the SfUploader it does send to the backend but the data i retrieve from the file stream in the backend is always empty.

Any ideas what i am doing wrong here and how i can fix this?

I have attached the example project with this code. To test just run both the API and BlazorApp at the same time and try the upload.


Thanks,

Bram



Attachment: blazor_sample_b2c25fc1.zip

4 Replies 1 reply marked as answer

BC Berly Christopher Syncfusion Team November 23, 2021 04:55 PM UTC

Hi Bram van Elderen, 
  
Greetings from Syncfusion support. 
  
We will validate the reported requirement and update the further details in two business days (25th November 2021). We appreciate your patience until then. 
  
Regards, 
Berly B.C 



BC Berly Christopher Syncfusion Team November 25, 2021 01:57 PM UTC

Hi Bram van Elderen, 
  
Thanks for the patience. 
  
We suggest you to convert the file stream to memory stream and read the text file as mentioned in the below code example. 
  
private async Task OnChange(UploadChangeEventArgs args) 
    { 
        UploadFiles file = args.Files.SingleOrDefault(); 
        //Due to FileInfo ambiguity reference between syncfusion and system.io added inline namespace for system.IO. 
        System.IO.FileInfo info = new System.IO.FileInfo(file.FileInfo.Name); 
            var stream = new MemoryStream(file.Stream.ToArray()); 
            using (StreamReader sr = new StreamReader(stream)) 
            { 
                var HtmlString = sr.ReadToEnd(); 
                var rteValue = HtmlString; 
            } 
    } 
 
  
Regards, 
Berly B.C 


Marked as answer

BV Bram van Elderen November 29, 2021 11:10 AM UTC

That worked thanks!



BC Berly Christopher Syncfusion Team November 30, 2021 05:47 AM UTC

Hi Bram van Elderen, 
  
Most welcome. Please get back to us if you need further assistance on this. 
  
Regards, 
Berly B.C 


Loader.
Up arrow icon