如何在运行时设置HttpPostedFileBase ContentType值 [英] how set HttpPostedFileBase ContentType value in runtime
本文介绍了如何在运行时设置HttpPostedFileBase ContentType值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在运行时设置HttpPostedFileBase ContentType值?
how set HttpPostedFileBase ContentType value in runtime?
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
//==============
推荐答案
非常感谢@StephenMuecke.通过将"ContentType"添加到以下函数中,即可解决该问题.
Many thanks to @StephenMuecke. By adding the 'ContentType' to the following function is solved.
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; }
}
这篇关于如何在运行时设置HttpPostedFileBase ContentType值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文