如何使用c#将byte []转换为HttpPostedFileBase

sar*_*nan 7 c# asp.net-mvc-4

如何使用c#将byte []转换为HttpPostedFileBase.在这里,我尝试了以下方式.

byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)bytes;
Run Code Online (Sandbox Code Playgroud)

我得到一个不能隐式转换错误.

Kar*_*son 20

如何创建自定义的发布文件?:)

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; }
}
Run Code Online (Sandbox Code Playgroud)

你可以这样使用:

byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(bytes);
Run Code Online (Sandbox Code Playgroud)