1.cshtml页面显示:
@using (Html.BeginForm("AjaxUpload", "DownLoad", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBox("fileData", "", new { type = "file", size = "25" })
<input type="submit" value="确定" />
}
必须要在form里进行标记 enctype="multipart/form-data"
2.mvc后台代码:
public void AjaxUpload(HttpPostedFileBase fileData)
{
string filename = fileData.FileName;
string path = Server.MapPath("~/upload");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
fileData.SaveAs(path+"/"+filename);
//Stream s = fileData.InputStream;
//byte[] buffer = new byte[s.Length];
//s.Read(buffer, 0, buffer.Length); //将流的内容读到缓冲区
//FileStream fs = new FileStream("文件路径", FileMode.OpenOrCreate, FileAccess.Write);
//fs.Write(buffer, 0, buffer.Length);
//fs.Flush();
//fs.Close();
}