Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 534
saravanan Avatar asked Aug 23 '16 07:08

saravanan


1 Answers

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);
like image 86
Abhishek Kanrar Avatar answered Oct 01 '22 17:10

Abhishek Kanrar