Get Upload file in memory stream

Hello Team!

Is there a way to get upload file into a memory stream? like this? I just want to get the file that the user select onto a memory stream because i have some other achievement with it!

<SfUploader ID="UploadFiles" AllowedExtensions=".pdf,.PDF">
    <UploaderEvents FileSelected="onsuccess"></UploaderEvents>
</SfUploader> 

public void onsuccess(SelectedEventArgs action)
    {
        string filePath = action.FilesData[0].RawFile.ToString(); // it always empty so not worked
        var memoryStream = new MemoryStream(File.ReadAllBytes(filePath));
               .....................
.....................................
    }

this code doesn't work so is there a possibility?

Thanks,
Chimène Nk.


8 Replies

PM Ponmani Murugaiyan Syncfusion Team April 6, 2020 07:37 AM UTC

Hello NKOUAMBIA, 
 
Greetings from Syncfusion support. 
 
We have validated your reported query. We have processed using onUploadStart event. In this event, the file return as raw file from file data and converted to byte[] from base64 and returned as memory stream. Please find the code snippet and sample link below for your reference.  
 
To convert memory stream :  
 
 
MemoryStream stream = new MemoryStream(byteArr);    
 
 
[Index.razor]  
 
<SfUploader ID="UploadFiles"> 
    <UploaderEvents OnUploadStart="uploadStart"></UploaderEvents> 
    <UploaderAsyncSettings SaveUrl="api/SampleData/Save" RemoveUrl="api/SampleData/Remove"></UploaderAsyncSettings> 
</SfUploader> 
 
@code{ 
    private void uploadStart(UploadingEventArgs args) 
    { 
        String[] splitStr = { ";base64," }; 
        var files = args.FileData.RawFile.ToString().Split(splitStr, 2, StringSplitOptions.RemoveEmptyEntries)[1]; 
        var byteArr = System.Convert.FromBase64String(files);    // return byte array 
        MemoryStream stream = new MemoryStream(byteArr);  // return memory stream 
    } 
} 
 
 
 
 
Kindly check with the above sample, whether meets your requirement. If not, please get back us, we will assist you further.  
 
Regards, 
Ponmani M 



GB Geraud Bertrand February 1, 2022 03:54 PM UTC

If I don't want to use a url, how could i get files ?



SP Sureshkumar P Syncfusion Team February 2, 2022 10:31 AM UTC

Geraud, 
 
You can get the uploading file by using Valuechange event without use a save/Remove url.  
 
Find the code example here: 
<SfUploader AutoUpload="false"> 
    <UploaderEvents ValueChange="OnChange"></UploaderEvents> 
</SfUploader> 
 
@code { 
 
    private void OnChange(UploadChangeEventArgs args) 
    { 
        foreach (var file in args.Files) 
        { 
            var path = @"path" + file.FileInfo.Name; 
            FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write); 
            file.Stream.WriteTo(filestream); 
            filestream.Close(); 
            file.Stream.Close(); 
        } 
    } 
} 
 
Regards, 
Sureshkumar P 



BD Boot Dat replied to Sureshkumar P May 23, 2022 10:28 PM UTC

Hi  Sureshkumar P,

I've been using the method you suggested and its been working great.
But i was wondering if i can do more with it,


You see, i save the path data as a string in the database, 
But that works for one upload at a time.

what i now wish fornis be able to upload multple files and bind their path string values to variables and be able to save them in the database.

 lets say, i want to upload 3 different images and be able to save these 3 images path string value to different  variables to the database. 

How can i achieve this ?



SP Sureshkumar P Syncfusion Team May 24, 2022 09:53 AM UTC

Query: lets say, i want to upload 3 different images and be able to save these 3 images path string value to different  variables to the database.

In our file uploader change event handler method, we have iterated multiple files using foreach looping. So, inside the foreach looping, you can save the required file into the specific patch to achieve your requirement.

Find the code example here:

   private void OnChange(UploadChangeEventArgs args) 

    { 

        foreach (var file in args.Files) 

        { 

         // you can save the individual file using different path as per your requirement here

            var path = @"path" + file.FileInfo.Name; 

            FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write); 

            file.Stream.WriteTo(filestream); 

            filestream.Close(); 

            file.Stream.Close(); 

        } 

    } 

 




BD Boot Dat June 5, 2022 05:47 PM UTC

Hi  Sureshkumar , 

Thanks for your response, but i'm not so sure if youve answered what i was asking..

apparently, i give path value to a variable, lets say image1 = path then i save the value string into database, 
but now i want to select and upload 3 images and for the file uploader to assign each image path to 3 variables, thus image1, image 2 and image3..

so when i save the 3 variables all saves each image path value. 

how i can achieve this ?

Also, 

is there a way i can change the image file name to a custom one before it's uploaded ?



SP Sureshkumar P Syncfusion Team June 6, 2022 11:52 AM UTC

Hi Boot,

As mentioned in our previous update, we have gotten the separate file value in the foreach iteration. So when saving the file into same patch you can change the specific path into the patch variable.

Find the code example here:

private void OnChange(UploadChangeEventArgs args)

    {

        foreach (var file in args.Files)

        {

            // get the specific image patch value and replace the image patch for the patch variable here

            // then upload the component.

            // in this loop you can get the each specific file in the file variable.

            var path = @"path" + file.FileInfo.Name;

            FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);

            file.Stream.WriteTo(filestream);

            filestream.Close();

            file.Stream.Close();

            List<string> DBPathVariable = new List<string>();

            DBPathVariable.Add(path);

        }

    }

Find the sample in the attachment:

Regards,

Sureshkumar P



SP Sureshkumar P Syncfusion Team June 6, 2022 11:54 AM UTC

find the sample in the attachment


Attachment: UploaderServer_a5334924.zip

Loader.
Up arrow icon