如何创建HttpPostedFileBase(或其继承类型)的实例

时间:2022-07-14 18:14:07

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.

目前我有一个包含图像文件的所有数据的byte [],只想构建一个HttpPostedFileBase的实例,这样我就可以使用现有的方法,而不是创建一个新的重载方法。

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
}

2 个解决方案

#1


37  

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.

现在,您可以传递此类的实例,其中需要HttpPostedFileBase。

#2


1  

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.

您无法手动创建HttpPostedFileBase或派生类(HttpPostedFile)的实例。该类仅应由框架实例化。你为什么不摆脱采用字节数组的第二个控制器动作?这不是必需的。使用HttpPostedFileBase的默认模型绑定器可以正常工作。

#1


37  

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.

现在,您可以传递此类的实例,其中需要HttpPostedFileBase。

#2


1  

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.

您无法手动创建HttpPostedFileBase或派生类(HttpPostedFile)的实例。该类仅应由框架实例化。你为什么不摆脱采用字节数组的第二个控制器动作?这不是必需的。使用HttpPostedFileBase的默认模型绑定器可以正常工作。