public async Task <IActionResult> Upload2([FromForm] IFormCollection forms) { StringValues pid = ""; StringValues userID = ""; StringValues information = ""; StringValues sPrice = ""; //需要绑定图片名和图片id StringValues[] temp = { "", "", "" }; string[] thagTag = { "tag", "tag1", "tag2" }; //int[5] a; for (int i = 0; i < 3; ++i) { forms.TryGetValue(thagTag[i], out temp[i]); } forms.TryGetValue("userId", out userID); forms.TryGetValue("pictureId", out pid); forms.TryGetValue("p_info", out information); forms.TryGetValue("price", out sPrice); int prices = int.Parse(sPrice); var LiPicture = context.picture.FirstOrDefault(s => s.p_id == pid); LiPicture.price = prices; LiPicture.p_info = information; context.picture.Attach(LiPicture); for (int i = 0; i < 3; ++i) { var tagPicture = new ownTag { p_id = pid, tag_name = temp[i] }; context.ownTag.Add(tagPicture); } return(Ok(new { Success = true, ownTag = temp, price = prices, p_info = information })); }
public async Task <IActionResult> Upload([FromForm] IFormCollection forms) { //只选取文件 FormFileCollection Lifile = (FormFileCollection)forms.Files; //需要绑定图片名和图片id StringValues[] temp = { "", "", "" }; string[] thagTag = { "tag", "tag1", "tag2" }; //int[5] a; for (int i = 0; i < 3; ++i) { forms.TryGetValue(thagTag[i], out temp[i]); } StringValues information = ""; forms.TryGetValue("p_Info", out information); StringValues userID = ""; forms.TryGetValue("userId", out userID); StringValues price = ""; forms.TryGetValue("price", out price); string tempsd = price; int Prices = int.Parse(tempsd); var files = Request.Form.Files; long size = files.Sum(f => f.Length); //size > 100MB refuse upload ! if (size > 104857600) { return(Ok(new { Success = false, Message = "pictures total size > 100MB , server refused !" })); } List <string> filePathResultList = new List <string>(); //只能上传一张图片顺便贴标签 var file = files[0]; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); //@ string filePath = "C:" + @"\Pics\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string suffix = fileName.Split('.')[1]; //检查文件后缀名确保是图片而不是其他文件。 if (!pictureFormatArray.Contains(suffix)) { return(Ok(new { Success = false, Message = "the picture format not support ! you must upload files " + "that suffix like 'png','jpg','jpeg','bmp','gif','ico'.", Name = fileName })); } //文件名命名? //存取图片的时候以id为准 //context.picture.Count(); fileName = Guid.NewGuid() + "." + suffix; string fileFullName = filePath + fileName; int height = 0; int width = 0; await using (FileStream fs = System.IO.File.Create(fileFullName)) { file.CopyTo(fs); System.Drawing.Image image = System.Drawing.Image.FromStream(fs); height = image.Height; width = image.Width; fs.Flush(); } filePathResultList.Add($"/src/Pics/{fileName}"); string message = $"{files.Count} file(s) /{size} bytes uploaded successfully!"; //Json("tag1", temp[0].ToString()); string OwnTags = "tag1:" + temp[0].ToString() + ",tag2:" + temp[1].ToString() + ",tag3:" + temp[2].ToString(); //刷新为服务器的图片。 fileFullName = "http://172.81.239.44:8002/" + fileName; picture tempPicture = new picture { p_id = (context.tableCount.Find(1).picture + 1).ToString(), p_url = fileFullName, p_info = information,//还是要能用http访问,不是https p_height = height, p_width = width, p_status = "OK",//图片状态不确定。 price = Prices, likes = 0, dislikes = 0, comm_num = 0 }; //添加图片数量 var tableCount = context.tableCount.Find(1); tableCount.picture += 1; context.tableCount.Attach(tableCount); context.SaveChanges(); //承接前一步异步保存。 context.picture.Add(tempPicture); await context.SaveChangesAsync(); //List<tag> contextTag = context.tag.ToList(); for (int i = 0; i < 3; ++i) { if (temp[i] != "") { tag isLegal = context.tag.FirstOrDefault(p => p.tag_name == temp[i]); if (isLegal == null) { //表示这是用户新增的tag isLegal = new tag { tag_name = temp[i] }; //因为外码依赖要先增加tag context.tag.Add(isLegal); await context.SaveChangesAsync(); } ownTag tempTag = new ownTag { p_id = tempPicture.p_id, tag_name = temp[i] }; context.ownTag.Add(tempTag); } } await context.SaveChangesAsync(); publishPicture tempPublish = new publishPicture { u_id = userID, p_id = tempPicture.p_id, publish_time = DateTime.Now }; context.publishPicture.Add(tempPublish); await context.SaveChangesAsync(); return(Ok(new { Success = true, Message = message, PictureHeight = tempPicture.p_height, PictureWidth = tempPicture.p_width, Price = Prices, PictureURL = tempPicture.p_url, //fileList = filePathResultList, OwnTag = OwnTags })); }