Hi, I am trying to get image URL from selected images. The challenge I am facing here is I am not supposed to touch the OnFilesChanged method directly. I need to create a separate class and access the original class items. How I can get image URL of each selected files in to a separate class list(my own class) I don't know this is the correct approach if not please suggest one
.below is my inaccessible method and inaccessible class Browsedfileclass
C#
private async Task OnFilesChanged(InputFileChangeEventArgs eventArgs)
{
var files = new List(eventArgs.FileCount);
files.AddRange(eventArgs.GetMultipleFiles(10000).Select(file => new Browsedfileclass(file)));
}so I have created a separate class like below
C#
public class Myclass
{
private Browsedfileclass file;
public Myclass(Browsedfileclass file)
{
File = file;
this.file = file;
Name = file.FileName;
ContentType = file.File.ContentType;
FileNameWithoutExtension = file.FileNameWithoutExtension;
Size = File.File.Size;
var image = File.File.RequestImageFileAsync("image/png", 100, 100);
var buffer = new byte[Size];
image.OpenReadStream().ReadAsync(buffer);//here i am getting error that OpenReadStream //doesnot contain
ImageDataUrl = $"data:{"image/png"};base64,{Convert.ToBase64String(buffer)}";
}
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 1, 2023, 6:42 AM EST
Hello Sujit,
Please change your code as below and try.
public class Myclass { private Browsedfileclass file; public Myclass(Browsedfileclass file) { this.file = file; Name = file.FileName; ContentType = file.File.ContentType; FileNameWithoutExtension = file.FileNameWithoutExtension; Size = file.File.Size; } public string Name { get; set; } public string ContentType { get; set; } public string FileNameWithoutExtension { get; set; } public int Size { get; set; } public string ImageDataUrl { get; set; } public async Task LoadImageDataUrl() { var image = file.File.RequestImageFileAsync("image/png", 100, 100); var buffer = new byte[Size]; using (var stream = await image) { await stream.ReadAsync(buffer, 0, buffer.Length); } ImageDataUrl = $"data:{"image/png"};base64,{Convert.ToBase64String(buffer)}"; } }private async Task OnFilesChanged(InputFileChangeEventArgs eventArgs) { var files = eventArgs.GetMultipleFiles(10000); var myFiles = new List(files.Length); foreach (var file in files) { var myFile = new Myclass(new Browsedfileclass(file)); await myFile.LoadImageDataUrl(); myFiles.Add(myFile); } }Thanks
Naimish
Sujeet RamanRank #1,329 · 1.3K reputationPosted Feb 2, 2023, 12:03 AM EST
I need to keep below lines anyway
var files = new List(eventArgs.FileCount); files.AddRange(eventArgs.GetMultipleFiles(10000).Select(file => new Browsedfileclass(file)));Getting error here
var myFiles = new List(files.Length);// Error :- List does not contain defenition of length //please see my class and i/f public class Browsedfileclass { public Browsedfileclass(IBrowsedFile file) { File = file ?? throw new ArgumentNullException(nameof(file)); FileName = Path.GetFileName(File.Name); } public IBrowsedFile File { get; } } public interface IBrowsedFile { string Name { get; } long Size { get; } string ContentType { get; } Stream OpenReadStream(long maxAllowedSize = 500 * 1024, CancellationToken cancellationToken = default); }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 5, 2023, 8:26 PM EST
I think the main reason you are getting an error in this line of code:
var myFiles = new List(files.Length);is that you are trying to create a new list of type "Myclass" using the length property of the "files" list which is of type "List".
To fix this error, you can simply replace "Myclass" with "Browsedfileclass" like this:
var myFiles = new List(files.Count);This will create a new list of type "Browsedfileclass" with the count of items in the "files" list.
Remember that the namespace for your "IBrowsedFile" interface is imported in your code file.