4

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.

Ramji
  • 375
  • 2
  • 14
Sardoan
  • 767
  • 1
  • 10
  • 25

1 Answers1

11
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?

CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
Sardoan
  • 767
  • 1
  • 10
  • 25