How to create an instance of HttpPostedFileBase(or its inherited type)

22,446

Solution 1

Create a derived class as follows:

class MemoryFile : HttpPostedFileBase
{
Stream stream;
string contentType;
string fileName;

public MemoryFile(Stream 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; }
}

public override void SaveAs(string filename)
{
    using (var file = File.Open(filename, FileMode.CreateNew))
        stream.CopyTo(file);
}
}

Now you can pass instance of this class where HttpPostedFileBase is expected.

Solution 2

You cannot manually create an instance of HttpPostedFileBase or derived classes (HttpPostedFile). This class is only supposed to be instantiated by the framework. Why don't you get rid of the second controller action that takes a byte array? It's not necessary. The default model binder will work fine with the one taking a HttpPostedFileBase.

Share:
22,446

Related videos on Youtube

Cheng Chen
Author by

Cheng Chen

500 Internal Server Error

Updated on April 05, 2020

Comments

  • Cheng Chen
    Cheng Chen about 4 years

    Currently I have a byte[] that contains all the data of an image file, just want to build an instance of HttpPostedFileBase so that I can use an existing method, instead of creating a new overload one.

    public ActionResult Save(HttpPostedFileBase file)
    
    public ActionResult Save(byte[] data)
    {
        //Hope I can construct an instance of HttpPostedFileBase here and then
        return Save(file);
    
        //instead of writing a lot of similar codes
    }
    
    • Devela
      Devela over 11 years
      did you get the answer to work for parsing the file stored in the byte[] ? Mine keeps the content disposition, etc.
  • Cheng Chen
    Cheng Chen over 12 years
    The js developer post me the byte[], this issue happens when user is pasting an image from the clipboard, instead of selecting an image file to upload.
  • Darin Dimitrov
    Darin Dimitrov over 12 years
    @Danny Chen, js sending a byte[]? That's seems very weird. What protocol is being used?
  • Cheng Chen
    Cheng Chen over 12 years
    He is sending me a base64 string, this behavior is performed by a 3rd party js editor.
  • Murat
    Murat over 8 years
    Just wanted to show how to use MemoryFile after you created: string filePath = Path.GetFullPath("C:\\images.rar"); FileStream fileStream = new FileStream(filePath, FileMode.Open); MemoryFile fileImage = new MemoryFile(fileStream, "application/x-rar-compressed", "images.rar");
  • Ibrahim Dauda
    Ibrahim Dauda about 5 years
    Tu es un genie! You, sir, are a genius!
  • Dan Diplo
    Dan Diplo about 5 years
    Just remember to close/dispose of that fileStream when you have finished otherwise you will leave the file permanently open (and inaccessible).