saravanan
saravanan

Reputation: 195

how to convert a byte[] to HttpPostedFileBase using c#

how to convert byte[] into HttpPostedFileBase using c#. here i have tried the following way.

byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)bytes;

I am getting an cannot implicitly convert error.

Upvotes: 10

Views: 24436

Answers (3)

Not Sure if this issue still exists or not, however when I have implemented the solution posted by @Zerratar I also encountered the same issue reported by @Jass on May 16, 2021. On checking the decompiled version of System.web I have found that SaveAs method need to be implemented whithout which saving will throw error as expected.

My Implementation is

public class FileInStreamFormat : HttpPostedFileBase
{
    private readonly byte[] fileBytes;
    private MemoryStream iStream;

    public FileInStreamFormat(byte[] fileBytes, string fileName, string ContentType)
    {
        this.fileBytes = fileBytes;
        this.FileName = fileName;
        this.ContentType = ContentType;
        this.iStream = new MemoryStream(fileBytes);
    }

    public override int ContentLength => fileBytes.Length;

    public override string FileName { get; }

    public override Stream InputStream
    {
        get { return iStream; }
    }

    public override string ContentType { get; }

    public override void SaveAs(string filename)
    {
        using (var file = File.Open(filename, FileMode.OpenOrCreate,FileAccess.Write))
            iStream.WriteTo(file);
    }
}

And the calling is

    public HttpPostedFileBase FileObjectStream(Stream fileStreamObj, string fileContentType)
    {
        MemoryStream newMs = new MemoryStream();
        fileStreamObj.CopyTo(newMs);
        byte[] fileByte = newMs.ToArray();
        FileInStreamFormat FISF = new FileInStreamFormat(fileByte, fileName, fileContentType);
        return FISF;
    }

Upvotes: 0

Abhishek Kanrar
Abhishek Kanrar

Reputation: 486

public class HttpPostedFileBaseCustom: HttpPostedFileBase
{
    MemoryStream stream;
    string contentType;
    string fileName;

    public HttpPostedFileBaseCustom(MemoryStream stream, string contentType, string fileName)
    {
        this.stream = stream;
        this.contentType = contentType;
        this.fileName = fileName;
    }

    public override int ContentLength
    {
        get { return (int)stream.Length; }
    }

    public override string ContentType
    {
        get { return contentType; }
    }

    public override string FileName
    {
        get { return fileName; }
    }

    public override Stream InputStream
    {
        get { return stream; }
    }

}

    byte[] bytes = System.IO.File.ReadAllBytes(localPath);
    var contentTypeFile = "image/jpeg";
    var fileName = "images.jpeg";
    HttpPostedFileBase objFile = (HttpPostedFileBase)new 
HttpPostedFileBaseCustom(new MemoryStream (bytes), contentTypeFile, fileName);

Upvotes: 3

Zerratar
Zerratar

Reputation: 1249

What about creating a custom postedfile? :)

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

That you can simply use like this:

byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(bytes);

Upvotes: 26

Related Questions