public async Task <object> FileUpload() { var httpRequest = HttpContext.Request; var imageUrls = string.Empty; var fileName = string.Empty; FilesReq dataReq = new FilesReq(); var res = new AuditUploadRes(); try { dataReq.path = Configuration["IMG_PATH"]; var uploads = Path.Combine(dataReq.path, "uploads"); if (!Directory.Exists(uploads)) { Directory.CreateDirectory(uploads); } dataReq.path = uploads; if (httpRequest.Form.Files.Count > 0) { for (var f = 0; f < httpRequest.Form.Files.Count; f++) { var postedFile = httpRequest.Form.Files[f]; var files = new List <IFormFile>(); files.Add(postedFile); foreach (var file in files) { fileName = ContentDispositionHeaderValue.Parse(postedFile.ContentDisposition).FileName.ToString().Trim('"'); var fileExtension = fileName.Substring(fileName.LastIndexOf(".")); var randomFileName = System.DateTime.Now.Ticks.ToString(); var finalFileName = randomFileName + fileExtension; string newPath = Path.Combine(dataReq.path, finalFileName); using (var fileStream = new FileStream(newPath, FileMode.Create)) { await file.CopyToAsync(fileStream); } res._result._code = "200"; res._result._message = ""; res._result._status = "OK"; res.fileName = finalFileName; res.path = dataReq.path; res.fullpath = newPath; } } } } catch (Exception ex) { res._result._code = "415 "; res._result._message = ex.Message; res._result._status = "Unsupported Media Type"; } return(res); }
public async Task <dynamic> UploadFile(IFormFile file) { var res = new AuditUploadRes(); FilesReq dataReq = new FilesReq(); try { dataReq.path = Configuration["IMG_PATH"]; var uploads = Path.Combine(dataReq.path, "fileupload"); if (!Directory.Exists(uploads)) { Directory.CreateDirectory(uploads); } dataReq.path = uploads; if (file != null && file.Length > 0) { var filePath = Path.Combine(dataReq.path, file.FileName); string extension = Path.GetExtension(file.FileName); string newFileName = Guid.NewGuid() + extension; string newPath = Path.Combine(dataReq.path, newFileName); using (var fileStream = new FileStream(newPath, FileMode.Create)) { await file.CopyToAsync(fileStream); } res._result._code = "200"; res._result._message = ""; res._result._status = "OK"; res.fileName = newFileName; res.path = dataReq.path; res.fullpath = newPath; } else { res._result._code = "204"; res._result._message = ""; res._result._status = "No Content"; } } catch (Exception ex) { res._result._code = "415 "; res._result._message = ex.Message; res._result._status = "Unsupported Media Type"; } return(res); }
[HttpPost("DownloadFile")] //download public async Task <dynamic> DownloadFile([FromBody] dynamic data) { FilesReq dataReq = new FilesReq(); dataReq.fileName = data != null ? data.fileName : null; dataReq.path = data != null && data.path ? data.path : Configuration["IMG_PATH"]; dataReq.file = Path.Combine(dataReq.path, dataReq.fileName); var memory = new MemoryStream(); using (var stream = new FileStream(dataReq.file, FileMode.Open)) { await stream.CopyToAsync(memory); } memory.Position = 0; return(File(memory, GetContentType(dataReq.file), dataReq.fileName)); }
[HttpPost("GetFile")] //download public async Task <dynamic> GetFile([FromBody] dynamic data) { string path = Configuration["IMG_PATH"]; FilesReq dataReq = new FilesReq(); dataReq.fileName = data != null?data.fileName:null; dataReq.path = data != null ? data.path: path; dataReq.fullpath = data != null ? data.fullpath : null; if (String.IsNullOrEmpty(dataReq.fullpath)) { dataReq.file = Path.Combine(dataReq.path, dataReq.fileName); } else { dataReq.file = Path.Combine(dataReq.fullpath); } return(new FileStream(dataReq.file, FileMode.Open, FileAccess.Read)); }
[HttpPost("deleteFile")] //download public void DeleteFile([FromBody] dynamic data) { string path = Configuration["IMG_PATH"]; FilesReq dataReq = new FilesReq(); dataReq.fileName = data != null ? data.fileName : null; dataReq.path = data != null ? data.path : path; dataReq.fullpath = data != null ? data.fullpath : null; if (String.IsNullOrEmpty(dataReq.fullpath)) { dataReq.file = Path.Combine(dataReq.path, dataReq.fileName); } else { dataReq.file = Path.Combine(dataReq.fullpath); } if ((System.IO.File.Exists(dataReq.file))) { System.IO.File.Delete(dataReq.file); } }
[HttpPost("SaveFile")] //download public async Task <dynamic> SaveFile([FromBody] dynamic data) { string path = Configuration["IMG_PATH"]; FilesReq dataReq = new FilesReq(); dataReq.fileName = data != null ? data.fileName : null; dataReq.path = data != null ? data.path : path; dataReq.fullpath = data != null ? data.fullpath : null; if (String.IsNullOrEmpty(dataReq.fullpath)) { dataReq.file = Path.Combine(dataReq.path, dataReq.fileName); } else { dataReq.file = Path.Combine(dataReq.fullpath); } if (!System.IO.File.Exists(dataReq.file)) { return(NotFound()); } string responsedata = String.Empty; var memory = new MemoryStream(); using (var stream = new FileStream(dataReq.file, FileMode.Open)) { //await stream.CopyToAsync(memory); byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); responsedata = Convert.ToBase64String(buffer); } memory.Position = 0; return(responsedata); }
[HttpPost("GetImage")] //download public async Task <dynamic> GetImage([FromBody] dynamic data) { string path = Configuration["IMG_PATH"]; FilesReq dataReq = new FilesReq(); dataReq.fileName = data != null ? data.fileName : null; dataReq.path = data != null ? data.path : path; dataReq.fullpath = data != null ? data.fullpath : null; if (String.IsNullOrEmpty(dataReq.fullpath)) { dataReq.file = Path.Combine(dataReq.path, dataReq.fileName); } else { dataReq.file = Path.Combine(dataReq.fullpath); } byte[] imageByteData = System.IO.File.ReadAllBytes(dataReq.file); string imageBase64Data = Convert.ToBase64String(imageByteData); string imageDataURL = string.Format("data:image/png;base64,{0}", imageBase64Data); return(imageDataURL); }
/// <summary> /// 加载微信素材 /// </summary> /// <returns></returns> internal string DownLoad() { string url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + base.Token(); bool isok = true; FileRes Totle = new FileRes() { Item = new List <FileItemRes>() }; int conut = 0; while (isok) { FilesReq req = new FilesReq(); req.Count = 20; req.Offset = conut; req.Type = "news"; string datas = JsonConvert.SerializeObject(req); string res = NetHelper.HttpRequest(url, datas, "POST", 6000, Encoding.UTF8, "application/json"); //得到素材 FileRes ones = JsonConvert.DeserializeObject <FileRes>(res); if (ones.Total_Count > conut) { //添加到数据中 Totle.Item.AddRange(ones.Item); conut += ones.Item_Count; if (conut == ones.Total_Count) { isok = false; } } else { Totle.Total_Count = ones.Total_Count; Totle.Item_Count = conut; isok = false; } } foreach (FileItemRes item in Totle.Item) { bool bo = _system.GetAllGraphicOne(item.Media_Id); if (!bo) { WXGraphicList list = new Hmj.Entity.WXGraphicList(); list.CreateDate = DateTime.Now; list.Merchants_ID = CurrentLoginUser.ORG_ID; list.Title = "";// Request["Name"]; list.Media_ID = item.Media_Id; int lid = 0; lid = _system.SaveGraphicList(list); foreach (FileItemNewsRes filse in item.Content.News_Item) { WXGraphicDetail CuObj = new Hmj.Entity.WXGraphicDetail(); // CuObj.Body = Request["ckeditor"];Media_ID CuObj.Describe = "微信同步"; CuObj.URL = filse.Url; CuObj.IsURL = true; CuObj.List_ID = lid; CuObj.Sorting = 0;// int.Parse(Request["Sorting"]); CuObj.Title = filse.Title; _system.SaveGraphicDetail(CuObj); } } } return(""); }