private static void UploadTestWithBase64() { while (true) { try { Console.WriteLine("please input file path and press enter"); var path = Console.ReadLine(); if (!System.IO.File.Exists(path)) { Console.WriteLine("不存在的文件"); break; } Base64File file = Base64File.FromFile(path, "dir2"); var result = new HttpClientUtil(uploadurl).PostWithJson <JsonResultData <FileInfo> >(uppath, file, new Dictionary <string, string> { ["Authorization"] = token }); Console.WriteLine("the result is :"); Console.WriteLine(result.Result); } catch (Exception ex) { Console.WriteLine(ex); } } }
private ResponseModel <MediaFile> WorkWithFileBase64(Base64File pBase64File) { ResponseModel <MediaFile> vResult; try { var convertFile = ConvertBase64ToFile(pBase64File.Base64String); if (!convertFile.IsOk) { throw new CustomException(convertFile.Code, convertFile.Message); } var getPreview = CreatePreview(convertFile.ReturnObject); if (!getPreview.IsOk) { throw new CustomException(getPreview.Code, getPreview.Message); } var saveFile = SaveFile(getPreview.ReturnObject); if (!saveFile.IsOk) { throw new CustomException(saveFile.Code, saveFile.Message); } vResult = getPreview; } catch (CustomException ce) { return(new ResponseModel <MediaFile> { Code = ce.ResponseCode, Message = ce.Message }); } catch (Exception e) { return(new ResponseModel <MediaFile> { Code = ResponseCodes.TECHNICAL_ERROR, Message = e.Message }); } return(vResult); }
public async Task <JsonResultData <FileInfo> > UploadWithBase64Content(Base64File file) { if (string.IsNullOrEmpty(file.Category)) { return(new JsonResultData <FileInfo>().SetFail("未设置子路径")); } if (string.IsNullOrEmpty(file.FileName)) { return(new JsonResultData <FileInfo>().SetFail("未设置文件名")); } var id = Guid.NewGuid().ToString("N"); var category = $"{FileServerConfigurationSection.Instance.WriteDir}\\{file.Category}"; var extension = file.FileName.Contains('.') ? "." + file.FileName.Split('.').Last() : null; var path = $"{category}\\{id}{extension}"; if (!System.IO.Directory.Exists(category)) { System.IO.Directory.CreateDirectory(category); } var bytes = Convert.FromBase64String(file.Base64Content); System.IO.File.WriteAllBytes(path, bytes); FileInfo fi = new FileInfo { Id = id, Size = bytes.Length, Category = file.Category, CreateTime = DateTime.Now, Extension = extension, Name = file.FileName, Url = $"{FileServerConfigurationSection.Instance.Name}\\{file.Category}\\{id}{extension}" }; await this.Cache.Add(fi); return(new JsonResultData <FileInfo> { Data = fi }.SetSuccess()); }
public async Task <IHttpActionResult> Upload([FromBody] Base64File file) { var result = await this._fileUploadService.UploadWithBase64Content(file); return(this.JsonResult(result.ToJsonResultData())); }