How to Create an Instance of HttpPostedFileBase?
Introduction
Some times we don't want to overload or change the existing upload file method that has the HttpPostedFileBase as parameter. And if we have byte[] that contains data of a file or we have base64 format of an image file, just want to build an instance of HttpPostedFileBase so that we can use an existing method, instead of creating a new overload one. This article may help you.
Detail
We have existing method as:
public ActionResult SaveFile(HttpPostedFileBase file)
{
//
}
And we want to use it as:
public ActionResult SaveMemoryFile(byte[] data)
{
//We will construct an instance of HttpPostedFileBase here and then
return SaveFile(file);
//instead of writing a lot of similar codes
}
For this we want to create an instance of HttpPostedFileBase. But it does not allow directly creating instance.
So we will create derived class for HttpPostedFileBase and override different methods.
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; }
}
public override void SaveAs(string filename)
{
using (var file = File.Open(filename, FileMode.CreateNew))
stream.WriteTo(file);
}
}
Now we can pass instance of this class where HttpPostedFileBase is expected.
public ActionResult SaveMemoryFile(byte[] data)
{
var contentType = "application/x-rar-compressed";
var fileName = "images.rar";
HttpPostedFileBaseCustom HttpPostedFileBaseCustom = new HttpPostedFileBaseCustom(GetSteamFromByeArray(data), contentType, fileName);
return SaveFile(HttpPostedFileBaseCustom);
//instead of writing a lot of similar codes
}
If we have base64 format of image then
public System.Web.Mvc.ActionResult SaveMemoryFile(string base64data)
{
var contentType = "Image/png";
var fileName = "mytestimage.png";
HttpPostedFileBaseCustom HttpPostedFileBaseCustom = new HttpPostedFileBaseCustom(GetSteamFromBase64String(base64data), contentType, fileName);
return SaveFile(HttpPostedFileBaseCustom);
//instead of writing a lot of similar codes
}
Other helper methods
public MemoryStream GetSteamFromBase64String(string imageBase64)
{
if (imageBase64.IndexOf(',') > 0)
{
imageBase64 = imageBase64.Substring(imageBase64.IndexOf(',') + 1);
}
byte[] bytes = Convert.FromBase64String(imageBase64);
var ms = new MemoryStream(bytes);
return ms;
}
public MemoryStream GetSteamFromByeArray(byte[] bytes)
{
var ms = new MemoryStream(bytes);
return ms;
}
Conclusion
In above discussion we see how to create an instance of HttpPostedFileBase and how to use it in different situations. Hope it will help you.
beginer
25-May-2018 at 09:04