如何使用c#将byte []转换为HttpPostedFileBase。在这里我尝试了以下方法。如何使用c#将字节[]转换为HttpPostedFileBase#
byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)bytes;
我得到一个不能隐式转换错误。
如何使用c#将byte []转换为HttpPostedFileBase。在这里我尝试了以下方法。如何使用c#将字节[]转换为HttpPostedFileBase#
byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)bytes;
我得到一个不能隐式转换错误。
创建自定义发布文件怎么样? :)
public class MemoryPostedFile : HttpPostedFileBase
{
private readonly byte[] fileBytes;
public MemoryPostedFile(byte[] fileBytes, string fileName = null)
{
this.fileBytes = fileBytes;
this.FileName = fileName;
this.InputStream = new MemoryStream(fileBytes);
}
public override int ContentLength => fileBytes.Length;
public override string FileName { get; }
public override Stream InputStream { get; }
}
,你可以简单地使用这样的:
byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(bytes);
'private Stream inputStream'行不需要,也没有使用。我试图编辑答案,但我发现[我们不应该编辑其他代码](http://meta.stackoverflow.com/a/276580/4826084)。 – Oskar
@Oskar你完全正确(关于不必要的inputStream字段)当我写答案时,我完全错过了。我会相应更新。谢谢你指出。 –
我在下面的代码中编译时遇到了下面的错误:Line:public override int ContentLength => fileBytes.Length;错误:fileBytes是一个字段,但像使用类型一样使用。请帮我解决@KarlPatrikJohansson – Ronak
的可能的复制http://stackoverflow.com/questions/28179730/is-it-possible-to-convert-byte- to-httppostedfile –