其他分享
首页 > 其他分享> > .NET API 上传图片

.NET API 上传图片

作者:互联网

   if (HttpContext.Current.Request.Files.Count > 0)
            {
                string pathbase = "/upload/order/" + DateTime.Now.ToString("yyyy/");
                string uploadpath = HttpContext.Current.Server.MapPath("~" + pathbase);//获取文件上传路径
                if (!Directory.Exists(uploadpath))
                {
                    Directory.CreateDirectory(uploadpath);
                }
              
                for (var i = 0; i < HttpContext.Current.Request.Files.Count; i++)
                {
                    var uploadFile = HttpContext.Current.Request.Files[i];
                    string newName = Guid.NewGuid().ToString() + Path.GetExtension(uploadFile.FileName);
                    //保存图片的逻辑
                    uploadFile.SaveAs(uploadpath + newName);
                    TAttachmentInfo attchInfo = new TAttachmentInfo()
                    {
                        FileOriginalName = uploadFile.FileName,
                        FileNewName = newName,
                        FilePath = pathbase + newName,
                        FileSize = uploadFile.ContentLength,
                        Creator = 0,
                        Status = StatusEnum.Active,
                        FileType = AttachmentFileTypeEnum.Image,
                        InsertTime = DateTime.Now,
                        RefID = 5,
                        RefType = RefTypeEnum.OrderEvaluationImg
                    };
                    itattachment.CreateTAttachment(attchInfo);
                    break;
                }
            }

标签:Files,uploadpath,uploadFile,newName,Current,API,NET,上传,HttpContext
来源: https://blog.csdn.net/Tusk_Act4/article/details/112352474