|
@using Syncfusion.Blazor.Inputs
@using System.IO;
@using System.ComponentModel.DataAnnotations
<EditForm Model="@employee" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
<DataAnnotationsValidator />
<div class="form-group">
<SfTextBox @bind-Value="@employee.EmpID"></SfTextBox>
</div>
<div class="form-group">
<SfUploader @ref="UploadObj" ID="UploadFiles">
<UploaderEvents ValueChange="OnChange"></UploaderEvents>
</SfUploader>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</EditForm>
@code{
SfUploader UploadObj;
public void OnChange(UploadChangeEventArgs args)
{
foreach (var file in args.Files)
{
var path = @"D:\" + file.FileInfo.Name;
FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);
file.Stream.WriteTo(filestream);
filestream.Close();
file.Stream.Close();
}
}
public Employee employee = new Employee();
public async Task HandleValidSubmit()
{
//you can upload the file after validate the form content using upload method
await this.UploadObj.Upload();
}
public void HandleInvalidSubmit()
{
}
public class Employee
{
[Required(ErrorMessage = "Employee ID is required")]
public string EmpID { get; set; }
}
}
|
|
|
|
<SfUploader @ref="UploadObj" ID="UploadFiles">
<UploaderEvents ValueChange="OnChange">UploaderEvents>
SfUploader>
public void OnChange(UploadChangeEventArgs args)
{
foreach (var file in args.Files)
{
var size = file.FileInfo.Size;
var path = @"D:\" + file.FileInfo.Name;
FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);
file.Stream.WriteTo(filestream);
filestream.Close();
file.Stream.Close();
}
}
|
Do we have any solution for this issue, we are also facing same issue.
Thanks
Hi
I suddenly have got this error, using the latesst version of Syncfusion. The same code has worked before
the stream field is null, eve tougth that the Upload function seems to be working as exspected.
I have tried both the sync and the async versions.
I dont get any error messages
Hi René,
We would like to inform you that there have been breaking changes in the 21.1.35 version onwards for the file stream argument for the Change event. We recommend referring to the release notes for more information on these changes. You can find the release notes on the Syncfusion Blazor documentation page here: https://blazor.syncfusion.com/documentation/release-notes/21.1.35?type=all#breaking-changes-7
We suggest using the updated code provided in the release notes to resolve the issue you are facing.
code changes here:
|
|
Regards,
Sureshkumar P
Thak you, This solved my issue.
We are happy to assist you. Please get back to us if you need any further assistance on this.
Hi Support Team,
I have adapted the implementation as mentioned, but I am receiving a JS error:
Microsoft.JSInterop.JSException
HResult=0x80131500
Message=Cannot read properties of null (reading '_blazorFilesById')
TypeError: Cannot read properties of null (reading '_blazorFilesById')
at Ze (https://localhost:5001/_framework/blazor.server.js:1:37264)
at Object.readFileData (https://localhost:5001/_framework/blazor.server.js:1:37222)
at https://localhost:5001/_framework/blazor.server.js:1:3244
at new Promise (<anonymous>)
at y.beginInvokeJSFromDotNet (https://localhost:5001/_framework/blazor.server.js:1:3201)
at Yt._invokeClientMethod (https://localhost:5001/_framework/blazor.server.js:1:61041)
at Yt._processIncomingData (https://localhost:5001/_framework/blazor.server.js:1:58516)
at Yt.connection.onreceive (https://localhost:5001/_framework/blazor.server.js:1:52157)
at s.onmessage (https://localhost:5001/_framework/blazor.server.js:1:80302)
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 812
at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.<OpenReadStreamAsync>d__29.MoveNext() in Microsoft.AspNetCore.Components.Forms\BrowserFileStream.cs:line 105
at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.<CopyFileDataIntoBuffer>d__30.MoveNext() in Microsoft.AspNetCore.Components.Forms\BrowserFileStream.cs:line 112
at System.Threading.Tasks.ValueTask`1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 816
at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult() in /_/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ValueTaskAwaiter.cs:line 126
at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.<ReadAsync>d__28.MoveNext() in Microsoft.AspNetCore.Components.Forms\BrowserFileStream.cs:line 98
at System.IO.Stream.<<CopyToAsync>g__Core|27_0>d.MoveNext() in /_/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs:line 108
at BlazorCommon.Components.Admin.BcAdminDomainPreOnboardingComponentBase.<OnFileUpload>d__21.MoveNext() in D:\develop.n8\Shared\BlazorMgtCommon\Components\Admin\BcAdminDomainPreOnboardingComponent.razor.cs:line 107
This exception was originally thrown at this call stack:
System.Threading.Tasks.ValueTask<TResult>.Result.get() in ValueTask.cs
Microsoft.AspNetCore.Components.Forms.BrowserFileStream.OpenReadStreamAsync(System.Threading.CancellationToken) in BrowserFileStream.cs
Microsoft.AspNetCore.Components.Forms.BrowserFileStream.CopyFileDataIntoBuffer(System.Memory<byte>, System.Threading.CancellationToken) in BrowserFileStream.cs
System.Threading.Tasks.ValueTask<TResult>.Result.get() in ValueTask.cs
System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult() in ValueTaskAwaiter.cs
Microsoft.AspNetCore.Components.Forms.BrowserFileStream.ReadAsync(System.Memory<byte>, System.Threading.CancellationToken) in BrowserFileStream.cs
System.IO.Stream.CopyToAsync.__Core|27_0(System.IO.Stream, System.IO.Stream, int, System.Threading.CancellationToken) in Stream.cs
BlazorCommon.Components.Admin.BcAdminDomainPreOnboardingComponentBase.OnFileUpload(Syncfusion.Blazor.Inputs.UploadChangeEventArgs) in BcAdminDomainPreOnboardingComponent.razor.cs
I am using V24.1.45 (migrated from latest V20)
Razor snippet:
<SfUploader AllowMultiple=false >
<UploaderEvents ValueChange=this.OnFileUpload />
</SfUploader>
CS snippet:
protected async void OnFileUpload(UploadChangeEventArgs args)
{
if (args.Files.Count != 1)
{
BcApplication.ShowMsgBox(this.BcaApp, "Please select a single file!");
return;
}
using MemoryStream oImportStream = new();
using Stream oUploadStream = args.Files[0].File.OpenReadStream(10 * 1024 * 1024);
await oUploadStream.CopyToAsync(oImportStream);
It doesn't matter if FileStream or MemoryStream is used as the target of the CopyToAsync.
I would appreciate it if you could point me to how to troubleshoot the issue.
Peter
Hi Peter,
Thank you for reaching out to us and providing details about the issue you're encountering. We understand the importance of resolving this matter promptly.
After carefully reviewing your query and attempting to replicate the issue on our end, we were unable to encounter the reported JS error. To better assist you, we kindly request additional details. If possible, could you share a more comprehensive code snippet that replicates the issue? Alternatively, you may modify the provided sample to reflect your scenario.
Additionally, a video illustration of the problem would greatly enhance our ability to pinpoint and address the root cause. Your cooperation in providing these details will enable us to offer a more accurate and prompt solution.
Sample: https://blazorplayground.syncfusion.com/VDBfNCVoUDfDRQKJ
Thank you for your understanding and collaboration. We are committed to resolving this matter to your satisfaction.
Regards,
Kokila Poovendran.