从流创建FormFile

时间:2018-11-28 14:13:16

标签: c# iformfile

我想在我的种子函数中创建一个FormFile。但是我得到了一个空引用异常。这是我的代码

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";

当我尝试设置内容类型时,将出现空引用。有任何想法我做错了吗?之前使用File.Exists进行过检查,该方法返回true。

1 个答案:

答案 0 :(得分:0)

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"
       };
}

已完成工作。似乎FormFile需要打开的流。在此处找到解决方案:How to instantiate an instance of FormFile in C# without Moq?

相关问题