asp.net core 上传文件controller保存
long size = 0; var files = Request.Form.Files; if (0 == Request.Form.Files.Count()) return Json("NoPicture"); foreach (var file in files) { var filename = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); filename = env.WebRootPath + $@"\{filename}"; size += file.Length; using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } }//IFormFile
var filename = formfile.FileName; filename = env.WebRootPath + $@"\{filename}"; size += formfile.Length; using (FileStream fs = System.IO.File.Create(filename)) { formfile.CopyTo(fs); fs.Flush(); }//database save the data which is not text
FileStream fs = new FileStream(@"D:\a.jpg", FileMode.Open,FileAccess.Read);
Byte[] btye2 = new byte[fs.Length]; fs.Read(btye2 , 0, Convert.ToInt32(fs.Length)); fs.Close(); using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "insert into Photo(imgfile) values(@imgfile)"; SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image); par.Value = bt; cmd.Parameters.Add(par); int t=(int)(cmd.ExecuteNonQuery()); conn.Close(); }