public string AddAttachMentPath(int userId) { var result = ""; try { if (userId <= 0) { var ex = new Exception("No User"); throw ex; } HttpFileCollection _file = context.Request.Files; if (_file.Count > 0) { var fileContent = _file["editormd-image-file"]; //文件大小 long size = fileContent.ContentLength; //文件类型 string type = fileContent.ContentType; //文件名 string name = fileContent.FileName; //文件格式 string _tp = Path.GetExtension(name); if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf") { var user_path = ""; var nowUser = userId.ToString(); for (int i = 0; i < nowUser.Length; i++) { user_path += nowUser[i] + "\\"; } //本站点的存储位置 var imagePath = "\\attachment\\blogimgs\\" + user_path; HttpPostedFile FileSave = fileContent; //用key获取单个文件对象HttpPostedFile string imgName = DateTime.Now.ToString("yyyyMMddhhmmss"); string imgPath = "uid" + userId + "_" + imgName + _tp; //通过此对象获取文件名 string AbsolutePath = HttpContext.Current.Server.MapPath(imagePath); if (!Directory.Exists(AbsolutePath)) //路径不存在就创建这个文件夹 { Directory.CreateDirectory(AbsolutePath); } var lastImagePath = AbsolutePath + imgPath; FileSave.SaveAs(lastImagePath);//将上传的东西保存 var url = context.Request.Url.Scheme + ":\\\\" + context.Request.Url.Host + ":" + context.Request.Url.Port + imagePath + imgPath; var ret = new { success = 1, message = "上传成功", url }; result = ExpandHelper.ToJsonString(ret); } } else { throw new Exception("No File"); } return(result); } catch (Exception ex) { return(ex.Message); } }