public string ImgUpload() { var imgId = ObjectId.GenerateNewId(); //读取文件 HttpPostedFile file = HttpContext.Current.Request.Files[0]; byte[] bs = new byte[file.ContentLength]; //比特流 file.InputStream.Read(bs, 0, bs.Length); //获取文件流 string exten = Path.GetExtension(file.FileName); //扩展名 string name = Guid.NewGuid().ToString() + exten; //图片名 Dnl_File ff = new Dnl_File(); ff.Bytes = bs; ff.FileType = "png"; ff.FileName = name; ff._id = imgId; ff.Size = bs.Length; ff.CreateAt = DateTime.Now.AddHours(8); MongoDBHelper.Instance.GetDnl_File().InsertOne(ff); //返回图片链接 string baseUrl = "http://211.154.6.166:9999"; string imgUrl = baseUrl + "/api/File/DownloadFile?fileId=" + imgId; return(imgUrl); }
public bool UploadFile(string fileId, string fileType, string fileName) { try { //读取文件 HttpPostedFile file = HttpContext.Current.Request.Files[0]; byte[] bs = new byte[file.ContentLength]; //比特流 file.InputStream.Read(bs, 0, bs.Length); //获取文件流 Dnl_File ff = new Dnl_File(); ff.Bytes = bs; ff.FileType = fileType; ff.FileName = fileName; ff._id = new ObjectId(fileId); ff.Size = bs.Length; ff.CreateAt = DateTime.Now.AddHours(8); MongoDBHelper.Instance.GetDnl_File().InsertOne(ff); return(true); } catch (Exception) { return(false); } }