mvc后台上传

public ActionResult AddEnclosure(HttpPostedFileBase Filedata)
{
if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0)// 没有文件上传,直接返回
{
return HttpNotFound();
}
//利用InputStream 属性直接从HttpPostedFile对象读取文本内容
System.IO.Stream MyStream;
int FileLen;
FileLen = Filedata.ContentLength;
// 读取文件的 byte[]
byte[] bytes = new byte[FileLen];
MyStream = Filedata.InputStream;
MyStream.Read(bytes, 0, FileLen);
// 保存文件到服务器
string strUrl = System.Web.HttpContext.Current.Server.MapPath(@"/FileBin/" + Path.GetFileName(Filedata.FileName));// 设置需要保存的地址
System.IO.FileStream fs = new System.IO.FileStream(strUrl, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
bw.Write(bytes);
bw.Flush();
fs.Flush();
bw.Close();
fs.Close();

原文地址:https://www.cnblogs.com/zhang102137/p/8384145.html