我想在我的种子函数中创建一个 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";
当我尝试设置内容类型时出现空引用。任何想法我做错了什么?之前使用返回 true 的 File.Exists 检查过。
最佳答案
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?
关于c# - 从流创建 FormFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53521428/