var localFile = Request.Form.Files[0];
注入hostingEnvironment
private IWebHostEnvironment hostingEnvironment;
public AdController(IWebHostEnvironment webHostEnvironment)
{
hostingEnvironment = webHostEnvironment;
}
获取url
string root = hostingEnvironment.WebRootPath + "/Upload/ADImages/";
string localExtennsion = System.IO.Path.GetExtension(localFilePath);// 后缀
string curFileName = DateTime.Now.ToString("yyyyMMddHHmmss");//文件名
var localFilePath = localFile.FileName;
string curExtennsion = localExtennsion;
curPath = root + curFileName + curExtennsion; //文件保存路径
FileStream fstream = new FileStream(curPath, FileMode.Create, FileAccess.ReadWrite);
//将上载文件的内容复制到目标流
localFile.CopyToAsync(fstream);
fstream.Flush();//必须加上这一行
到此结束nice
偶尔发布一下小问题,觉得有用的点个赞吧!