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. Image for the cookie policy date

Cannot send MemoryStream to server via PostAsync request

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

3 Replies

PK Prem Kumar Madhan Raj Syncfusion Team 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 



ER Eric 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! :)


PK Prem Kumar Madhan Raj Syncfusion Team 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 


Loader.
Live Chat Icon For mobile
Up arrow icon