I have a blazor server app which uses the SfUpload component. I noticed few minor inconveniences when using the component.
Random file validation fail - The component will randomly return that a file type is not allowed. Issue is that I have not specified this parameter hence all files should be allowed. Regardless I noticed that the file is being uploaded anyway.
The same issue happens but with an error message "File failed to upload", file is being uploaded successfully but the component returns an error.
I noticed this behaviour mostly when dealing with .png and .zip files that contain .png.
My code:
<SfUploader AutoUpload="true" AllowMultiple="true">
<Syncfusion.Blazor.Inputs.UploaderEvents ValueChange="OnUpload"></Syncfusion.Blazor.Inputs.UploaderEvents>
</SfUploader>
// Method do upload files to local folder
void OnUpload(Syncfusion.Blazor.Inputs.UploadChangeEventArgs args)
{
currentDirectory = this.sfFileManager?.Path ?? "/";
foreach (var file in args.Files)
{
string path = $"{basePath}/{currentDirectory}/{file.FileInfo.Name}";
if (File.Exists(path))
{
File.Delete(path);
}
FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);
file.Stream.WriteTo(filestream);
filestream.Close();
file.Stream.Close();
}
}
[Update]
I noticed that files are actually not uploaded successfully but instead empty files are created in target directory.
Hi Michal,
Based on the shared information, we suspect that you are using older code. We would like to inform you that there are breaking changes on our end. Previously, the UploadChangeEventArgs.Stream property would return the memory stream. However, in this release, the UploadChangeEventArgs.Stream property will return null instead of the memory stream. The Stream property in the UploadFiles class has been deprecated and will no longer be used. It will be removed in a future version. You can create a stream manually by calling the OpenReadStream method. 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 = @"D:\" + 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
Additionally, we have also included the fix for "When uploading files, an error message is displayed stating 'File type is not allowed'" in the latest version 22.1.36. Therefore, we suggest that you upgrade to our latest version in order to resolve this current issue.
If you are still facing the issue, we request that you provide a issue reproducible sample This will help us validate the issue further and provide you with a better solution.
Regards,
Mallesh
Hello
I updated my Suncfusion Package to 22.1.37 and my code to follow your sample:
public static async Task<string>? FileUpload(UploadChangeEventArgs args, string folder = "")
{
string str1 = DateTime.Now.GetHashCode().ToString();
string? str2 = null;
string targetDirectory = Environment.CurrentDirectory + $"/Files/{(string.IsNullOrEmpty(folder) ? "" : folder + "/")}" + str1;
if (Directory.Exists(targetDirectory) is false)
{
Directory.CreateDirectory(targetDirectory);
}
foreach (UploadFiles file in args.Files)
{
string interpolatedStringHandler = $"{targetDirectory}{file.FileInfo.Name}";
string stringAndClear = interpolatedStringHandler.Trim();
if (File.Exists(stringAndClear))
File.Delete(stringAndClear);
FileStream fileStream = new FileStream(stringAndClear, FileMode.Create, FileAccess.Write);
await file.File.OpenReadStream(long.MaxValue).CopyToAsync(fileStream);
fileStream.Close();
str2 = $"{targetDirectory}" + file.FileInfo.Name;
}
return str2 ?? string.Empty;
}
Files are uploaded correctly but the issue with "File type is not allowed" is still present. Randomly a file will fail to upload with this error but if I refresh the website all is fine. So far I noticed this only happens on .png and .z
Hi Michal Poterek,
After thorough testing, we were unable to reproduce the issue you described in the latest version. When attempting to upload PNG files, we found that the files uploaded successfully without any errors. Please refer to the attached sample and video for further reference. To provide further assistance, we kindly request the following details from you:
Could you please share a sample that reproduces the issue? This will greatly assist us in identifying the cause and finding an appropriate solution. If possible, modify the sample that was shared to reflect the problem accurately.
It would be helpful if you could provide us with the specific uploaded PNG image that resulted in the issue. This will allow us to analyze the file and investigate any potential compatibility or processing concerns.
Regards,
Yohapuja S
This issue is still present.Sometimes single users on our deployed site will not be able to upload because it says."
File type is not allowed"
The filetype is totally valid and works almost all of the time. There must be something going on here..
This is a persistent issue. A re-deployment of the site fixes the problem. Error handling does not fire because no error has occurred.
Code wise:
private async Task OnUploadAsync(UploadChangeEventArgs args)
{
foreach (var file in args.Files)
{
MemoryStream memory = new MemoryStream();
await file.File.OpenReadStream(long.MaxValue).CopyToAsync(memory);
ImageToUpload = memory.ToArray();
MediaItem.ImageData = ImageToUpload;
}
}
Hi fateracepp
We want to acknowledge you that there has been an adjustment in the behavior of the UploadChangeEventArgs.Stream property, which in the past would provide a memory stream. However, in the current version, it will return null rather than the memory stream. It's important to note that this particular property within the UploadFiles class is now marked as deprecated and will eventually be phased out in upcoming releases.
To adapt to this change, we recommend taking a manual approach by utilizing the OpenReadStream method to create a stream. Below, we've included an example code snippet to help illustrate this transition:
Code Snippet:
|
private async Task OnChange(UploadChangeEventArgs args) { try { foreach (var file in args.Files) { var path = @"" + file.FileInfo.Name; FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write); await file.File.OpenReadStream(long.MaxValue).CopyToAsync(filestream); filestream.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } |
Documentation: https://blazor.syncfusion.com/documentation/file-upload/getting-started#save-and-remove-actions
Feedback: https://www.syncfusion.com/feedback/42988/file-upload-stream-issue
Can you please confirm whether the change you made has resolved the "file type is not allowed" error?
Regards,
Yohapuja S
I don't want to save the file locally though, I want it to save to a byte array. I am using OpenReadStream and it works 99% of the time!
This is happening intermittently. My above code is fully functional but the issue is with the control.
Can you clairfy which method is deprecated?
I am already using .OpenReadStream().
Hi Michal Poterek,
Query1: "I don't want to save the file locally though, I want it to save to a byte array. I am using OpenReadStream and it works 99% of the time! "
Thank you for sharing your requirement. If your intention is to store the files directly into a byte array, we suggest referring to the following forums for detailed discussions:
https://www.syncfusion.com/forums/173160/resize-uploaded-image-and-store-it-as-byte?reply=S2QKBX
https://www.syncfusion.com/forums/172140/file-upload-style?reply=S2CNHP
Query2: "Can you clairfy which method is deprecated? "
We would like to provide clarity on the deprecated properties starting from version 21.1.35:
UploadChangeEventArgs.Stream property would return the memory stream. However, in this release, the UploadChangeEventArgs.Stream property will return null instead of the memory stream. This Stream property in UploadFiles class has been deprecated and will no longer be used. It will be removed in a future version.Event property in CancelEventArgs , FailureEventArgs , PauseResumeEventArgs , RemovingEventArgs , SelectedEventArgs , SuccessEventArgs classes has been deprecated and will no longer be used. It will be removed in a future version.E property in FailureEventArgs , ProgressEventArgs , SuccessEventArgs classes has been deprecated and will no longer be used. It will be removed in a future version.
For more information please refer to the below UG documentation
https://blazor.syncfusion.com/documentation/release-notes/21.1.35?type=all#file-upload
Regards,
Priyanka K