I want to create a FormFile in my seed function. But I get a null reference exception. Here my code
FileStream fs = new FileStream("testfile.jpg", FileMode.Open, FileAccess.Read);
FormFile file = new FormFile(fs, 1024, fs.Length, "testfile", "testfile.jpg");
file.ContentType = "image/jpeg";
The null reference occurs when I am trying to set content type. Any Ideas what I am making wrong? Checked before with File.Exists which returns true.
using (var stream = File.OpenRead("testfile.jpg"))
{
FormFile file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
{
Headers = new HeaderDictionary(),
ContentType = "image/jpeg"
};
}
Did the jobs. Seems FormFile needs an opened stream.Found the solution here: How to instantiate an instance of FormFile in C# without Moq?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With