In my code I have a OnPost()
method that recieves an IFormCollection
, through this I pass a few files through. One of these files I always want to always be the the index[0] of a List<byte[]>
. Is there a way for me to convert this List<IFormFile>
back to either the original IFormCollection
or even to the IFormFileCollection
?
Here is my code:
// Access form data
string? clientName = formData["client-name"]!;
string? comments = formData["comments"]!;
string? injury = formData["injury"]!;
var fileName = string.Empty;
List<byte[]> bytes = new List<byte[]>();
var blob = formData.Files["blob"];
// Access uploaded files
IFormFileCollection files = formData.Files;
if (blob != null && blob.Length > 0)
{
using (var stream = blob.OpenReadStream())
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
byte[] blobBytes = memoryStream.ToArray();
bytes.Add(blobBytes);
List<IFormFile> fileList = files.ToList();
int indexRemover = fileList.FindIndex(file => file.FileName == "blob.png");
if(indexRemover == -1)
{
fileList.RemoveAt(indexRemover);
}
}
}
Note: This isn’t the whole method just the part where I am getting stuck. Go off the basis of the method looking like this:
public IActionResult OnPost(IFormCollection formData) {
-- Previous code --
return Page()
}
There is more to the method than this but for simplicity and straightforwardness I just included what I felt were the necessary pieces. If you need more information just let me know.