如何在运行时设置HttpPostedFileBase ContentType值?
HttpPostedFileBase upl=null;
string path="/exelFile/book1.xlsx";
//-----Set Name Runtime
var Name="FileName.xlsx";
//-----Set Type Runtime
var type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
byte[] bytes =System.IO.File.ReadAllBytes(Server.MapPath(Path));
upl = (HttpPostedFileBase)new MemoryPostedFile(bytes, Name);
//=====>how set type
upl.ContentType
//==============
答案 0 :(得分:1)
非常感谢@StephenMuecke。通过将'ContentType'添加到以下函数中解决了。
public class MemoryPostedFile : HttpPostedFileBase
{
private readonly byte[] fileBytes;
public MemoryPostedFile(byte[] fileBytes, string fileName = null,string ContentType=null)
{
this.fileBytes = fileBytes;
this.FileName = fileName;
this.ContentType = ContentType;
this.InputStream = new MemoryStream(fileBytes);
}
public override int ContentLength => fileBytes.Length;
public override string FileName { get; }
public override string ContentType { get; }
public override Stream InputStream { get; }
}