如何 post 文件到控制器中的变量(类型为 'HttpPostedFileBase' )
How to post a file to a variable (with type 'HttpPostedFileBase' ) in controller
我的模型中有一个 HttpPostedFileBase
作为 type 的变量。型号如下:
public class MailModel
{
public int mail_id { get; set; }
public string From { get; set; }
public string To { get; set; }
public string subject { get; set; }
public string Content { get; set; }
public HttpPostedFileBase file { get; set; }
}
现在我想从我的 local file path 中为变量 file
赋值。如何在相应的控制器中给file
赋值?
public class MailController : Controller
{
MailModel mm = new MailModel();
mm.file = ? //Can I add a filepath?
}
谢谢!
终于找到解决办法了。我已使用以下代码将文件路径转换为字节:
byte[] bytes = System.IO.File.ReadAllBytes(FilePath);
我在 MailModel
中为 HttpPostedFileBase
创建了派生的 class。
public class MemoryPostedFile : HttpPostedFileBase
{
private readonly byte[] FileBytes;
private string FilePath;
public MemoryPostedFile(byte[] fileBytes, string path, string fileName = null)
{
this.FilePath = path;
this.FileBytes = fileBytes;
this._FileName = fileName;
this._Stream = new MemoryStream(fileBytes);
}
public override int ContentLength { get { return FileBytes.Length; } }
public override String FileName { get { return _FileName; } }
private String _FileName;
public override Stream InputStream
{
get
{
if (_Stream == null)
{
_Stream = new FileStream(_FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
}
return _Stream;
}
}
private Stream _Stream;
public override void SaveAs(string filename)
{
System.IO.File.WriteAllBytes(filename, System.IO.File.ReadAllBytes(FilePath));
}
}
然后我使用以下代码从 MailController 调用它:
public class MailController: Controller
{
byte[] bytes = System.IO.File.ReadAllBytes(FilePath);
MailModel model= new MailModel();
model.file = (HttpPostedFileBase)new MemoryPostedFile(bytes, FilePath, filename);
}
现在我可以为变量 "file" 赋值(类型为 HttpPostedFileBase)
我的模型中有一个 HttpPostedFileBase
作为 type 的变量。型号如下:
public class MailModel
{
public int mail_id { get; set; }
public string From { get; set; }
public string To { get; set; }
public string subject { get; set; }
public string Content { get; set; }
public HttpPostedFileBase file { get; set; }
}
现在我想从我的 local file path 中为变量 file
赋值。如何在相应的控制器中给file
赋值?
public class MailController : Controller
{
MailModel mm = new MailModel();
mm.file = ? //Can I add a filepath?
}
谢谢!
终于找到解决办法了。我已使用以下代码将文件路径转换为字节:
byte[] bytes = System.IO.File.ReadAllBytes(FilePath);
我在 MailModel
中为 HttpPostedFileBase
创建了派生的 class。
public class MemoryPostedFile : HttpPostedFileBase
{
private readonly byte[] FileBytes;
private string FilePath;
public MemoryPostedFile(byte[] fileBytes, string path, string fileName = null)
{
this.FilePath = path;
this.FileBytes = fileBytes;
this._FileName = fileName;
this._Stream = new MemoryStream(fileBytes);
}
public override int ContentLength { get { return FileBytes.Length; } }
public override String FileName { get { return _FileName; } }
private String _FileName;
public override Stream InputStream
{
get
{
if (_Stream == null)
{
_Stream = new FileStream(_FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
}
return _Stream;
}
}
private Stream _Stream;
public override void SaveAs(string filename)
{
System.IO.File.WriteAllBytes(filename, System.IO.File.ReadAllBytes(FilePath));
}
}
然后我使用以下代码从 MailController 调用它:
public class MailController: Controller
{
byte[] bytes = System.IO.File.ReadAllBytes(FilePath);
MailModel model= new MailModel();
model.file = (HttpPostedFileBase)new MemoryPostedFile(bytes, FilePath, filename);
}
现在我可以为变量 "file" 赋值(类型为 HttpPostedFileBase)