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

Unable to run latest file uploader example

I'm using latest package but I'm not able to run the latest file uploader example.

(previously I was able to use the control but I realize there were breaking changes and it must be done differently now)

var path2 = Path.Combine(@"C:\" + file2.FileInfo.Name);

FileStream filestream2 = new FileStream(path2, FileMode.Create, FileAccess.Write);

await file.File.OpenReadStream(long.MaxValue).CopyToAsync(filestream2);

filestream2.Close();

Error occurs at file.File.OpenReadStream(long.MaxValue).CopyToAsync(filestream2);

With the error:

Message = 'Cannot read properties of null (reading '_blazorFilesById')TypeError: Cannot read properties of null (reading '_blazorFilesById') at He (https://localhost:44309/_framework/blazor.webassembly.js:1:33094) at Object.readFileData (https://localhost...


Can you provide code that you know works that will get the contents into a string?

In other words, the old way was this:

// var contents = Encoding.UTF8.GetString(file.Stream.ToArray());

What is the new way? I've tried seemingly everything.


FYI, I also tried:

var reader5 = await new StreamReader(file.File.OpenReadStream()).ReadToEndAsync();


But received the same error.


Thanks




4 Replies

PK Priyanka Karthikeyan Syncfusion Team April 11, 2023 01:32 PM UTC

Hi steve,


We were unable to reproduce the reported issue in our end. We have prepared a sample for your reference. Please find the example code and sample for your reference.


private async void OnChangeImage(UploadChangeEventArgs args)

        {

        try

        {

            foreach (var file in args.Files)

            {

                var path = @"E:\" + file.FileInfo.Name;

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

                // Calls the OpenReadStream method on the uploaded file to get a read stream

                await file.File.OpenReadStream(long.MaxValue).CopyToAsync(filestream);

                filestream.Close();

            }

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.Message);

        }

    }


Find the documentation link for your reference:
https://blazor.syncfusion.com/documentation/release-notes/21.1.35?type=all#file-upload


If you are still facing the issue, please share a sample with us so that we can reproduce the issue on our end.

Regards,

Priyanka K



Attachment: EditTest_663bb929.zip



SD Steve DeLollis April 11, 2023 05:48 PM UTC

Thanks for checking.


Are you testing with a Blazer Server app?


I'm using WASM.


I actually only need to get the contents to a string, I'd think by using this code:


var reader5 = await new StreamReader(file.File.OpenReadStream()).ReadToEndAsync();


As I mentioned this was the old code that worked:


// var contents = Encoding.UTF8.GetString(file.Stream.ToArray());





SD Steve DeLollis April 11, 2023 06:52 PM UTC


After searching I tried something else but get an error. Tried:


var stream3 = file2.File.OpenReadStream(long.MaxValue);  // File is simple csv delimited text file.

MemoryStream ms = new MemoryStream();

await stream3.CopyToAsync(ms);    // *** error occurs here

stream3.Close();

var content3 = ms.ToArray();

ms.Close();


My questions now are:

Can you run this from a blazor WASM app? 

How can I get the contents of a file to a string (which was working previously)


***

'Cannot read properties of null (reading '_blazorFilesById')\nTypeError: Cannot read properties of null (reading '_blazorFilesById')\n at He (https://localhost:44309/_framework/blazor.webassembly.js:1:33094)\n at Object.readFileData (https://localhost:44309/_framework/blazor.webassembly.js:1:33052)\n at https://localhost:44309/_framework/blazor.webassembly.js:1:3337\n at new Promise (<anonymous>)\n at Object.beginInvokeJSFromDotNet (https://localhost:44309/_framework/blazor.webassembly.js:1:3311)\n at Object.Gt [as invokeJSFromDotNet] (https://localhost:44309/_framework/blazor.webassembly.js:1:62569)\n at Object.Ii (https://localhost:44309/_framework/dotnet.7.0.4.86mgmbrr93.js:5:71922)\n at _mono_wasm_invoke_js_blazor (https://localhost:44309/_framework/dotnet.7.0.4.86mgmbrr93.js:14:103886)\n at wasm://wasm/00992dae:wasm-function[313]:0x1d507\n at wasm://wasm/00992dae:wasm-function[283]:0x1c935'



PK Priyanka Karthikeyan Syncfusion Team April 13, 2023 08:10 AM UTC

Hi Steve,


You need to pass maximum size to the OpenReadStream to convert file content to string. Please find the example code and sample for your reference.


private async void OnChangeImage(UploadChangeEventArgs args)

        {

        try

        {

            foreach (var file in args.Files)

            {

                var path = @"E:\" + file.FileInfo.Name;

                var reader5 = await new StreamReader(file.File.OpenReadStream(long.MaxValue)).ReadToEndAsync();

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

                // Calls the OpenReadStream method on the uploaded file to get a read stream

                await file.File.OpenReadStream(long.MaxValue).CopyToAsync(filestream);

                filestream.Close();

            }

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.Message);

        }

    }


Find the documentation link for your reference: https://blazor.syncfusion.com/documentation/release-notes/21.1.35?type=all#file-upload


Regards,

Priyanka K




Attachment: 
UploadWebAssembly_(3)_30b4eff9.zip


Attachment: 230413_113634_video_240f653f.zip


Loader.
Up arrow icon