We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Cannot send MemoryStream to server via PostAsync request

Thread ID:

Created:

Updated:

Platform:

Replies:

147807 Sep 24,2019 06:33 PM UTC Sep 26,2019 05:27 AM UTC Blazor 3
loading
Tags: File Manager
Eric
Asked On September 24, 2019 06:33 PM UTC

As this question is not exclusive to syncfusion, not sure you will waste your time answering, but here it goes => How can I send a MemoryStream created on the client side to the server side?

I am able to create and download (SaveAs) a powerpoint presentation at the client side with no issues, or create and send via email the presentation on the server side. What I want to do is be able to create the presentation and send the Stream to the server side so I just need to attach it to the email and send it.

I have tried PostAsync and PostJsonAsync (with multiple variations) to no avail. Any help would be appreciated.

Best,
Eric

Prem Kumar Madhan Raj [Syncfusion]
Replied On September 25, 2019 02:11 PM UTC

Hi Eric, 
 
Thanks for contacting Syncfusion support. 
 
We have analyzed with your query to send the memory stream from client side to the server side controller method at our end. Unfortunately, we can’t post the memory stream directly from the client end to the server controller. However, we can convert the memory stream to a base 64 string and post it from the client to the server controller method and then convert the obtained base 64 string obtained in the server controller method into memory stream to achieve your requirement. As shown in the below code snippet. 
 
[Index.razor] 
 
//Converting a memory stream to base 64 string 
string baseString = Convert.ToBase64String(stream.ToArray()); 
//Send the base 64 text 
await Http.SendJsonAsync(HttpMethod.Post, "/api/Sample/Sample", new PostValue { value = baseString }); 
 
 
[sampleController] (server side controller) 
 
[HttpPost("[Action]")] 
        public void Sample([FromBody] PostValue value) 
        { 
            //converting the base 64 string back to memory stream 
            MemoryStream sample = new MemoryStream(Convert.FromBase64String(value.value)); 
            //saving the file locally in the machine from the memory stream 
            FileStream fs; 
            using (fs = new FileStream("output.pdf", FileMode.OpenOrCreate)) 
            { 
                sample.CopyTo(fs); 
            } 
        } 
 
 
For your convenience, we have attached the sample to achieve your requirement as explained above in the below link. Please check it. 
 
 
Here, in the sample, we have converted a simple pdf file memory stream (been created dynamically) to a base 64 string at the client end and converted the same base 64 string to a memory stream obtained in the server controller method and saved pdf file in the (Server directory) as output.pdf . Please let us know if you need any further assistance. 
 
Regards, 
 
Prem Kumar M 


Eric
Replied On September 25, 2019 06:43 PM UTC

Thank you so much, this is exactly what I wanted! Even passing as part of an object, which I had not mentioned, is here. I really appreciate your help! :)

Prem Kumar Madhan Raj [Syncfusion]
Replied On September 26, 2019 05:27 AM UTC

Hi Eric, 
 
Thanks for the update. We are happy to hear that the provided solution met your requirement. Please let us know if you need any further assistance.

Regards,
 
 
Prem Kumar M 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon