public FileUploadResult Upload(string base64String) { try { string directory = $"StuAvatar/Images"; FileManageClient client = new FileManageClient(ConfigurationManager.AppSettings["uploadUrl"]); //base64String = base64String.Replace("data:image/png;base64,", string.Empty); base64String = base64String.Split(',')[1]; string fileName = Guid.NewGuid().ToString("N") + ".png"; //Base64形式的String转成byte[] var imgByte = Convert.FromBase64String(base64String); MemoryStream fs = null; FileUploadResult response = null; using (fs = new MemoryStream(imgByte)) { response = client.Upload(fileName, fs, ConfigurationManager.AppSettings["OssSubject"], directory, true); } if (response.Code == FileManageStatusCode.Success) { response.FileUrl = $"/{directory}/{response.FileName}"; } return(response); } catch (Exception ex) { throw new Exception("网络出错了,头像上传失败"); } }
public ActionResult Upload(HttpPostedFileBase file) { var extension = Path.GetExtension(file.FileName).ToLower(); var fileType = ""; if (extension == ".jpg" || extension == ".png" || extension == ".gif" || extension == ".bmp") { fileType = "images"; } else if (extension == ".mp4") { fileType = "video"; } else if (extension == ".mp3") { fileType = "audio"; } else { return(Json(new JsonSimpleResponse() { State = false, ErrorMsg = "选择的文件类型有错误" })); } FileManageClient client = new FileManageClient(ConfigurationManager.AppSettings["uploadUrl"]); var response = client.Upload(file.FileName, file.InputStream, ConfigurationManager.AppSettings["OssSubject"], $"SmartChinese/{fileType}", true); return(Json(response)); }
public override void Process() { string uploadFileName = null; var file = Request.Files[UploadConfig.UploadFieldName]; uploadFileName = file.FileName; try { string uploadUrl = ConfigurationManager.AppSettings["uploadUrl"]; string subject = ConfigurationManager.AppSettings["OssSubject"]; string topDirectory = ConfigurationManager.AppSettings["TopDirectoryOfSubject"]; Result.OriginFileName = uploadFileName; FileManageClient client = new FileManageClient(uploadUrl); FileUploadResult result = client.Upload( uploadFileName, file.InputStream, subject, topDirectory); Result.Url = result.FileUrl; Result.State = UploadState.Success; } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } finally { WriteResult(); } }
private FileUploadResult Upload(byte[] audio) { var fileType = FileTypeEnum.Audio.ToString(); var fileName = Guid.NewGuid().ToString("N") + ".mp3"; string directory = $"Resource/{fileType}"; Stream stream = new MemoryStream(audio); FileManageClient client = new FileManageClient(ConfigurationManager.AppSettings["uploadUrl"]); var response = client.Upload(fileName, stream, ConfigurationManager.AppSettings["OssSubject"], $"{directory}", true); if (response.Code == FileManageStatusCode.Success) { response.FileUrl = $"/{directory}/{response.FileName}"; } return(response); }
public ActionResult Upload(HttpPostedFileBase file) { //string url = SaveFile(file); //return Json(new { url = url }); var fileTypeStr = ""; var type = FileTypeEnum.UnKnow; try { type = FileHelper.CheckFile(file); if (type != FileTypeEnum.UnKnow) { fileTypeStr = type.ToString(); } else { return(Json(new JsonSimpleResponse() { State = false, ErrorMsg = "选择的文件类型有错误" })); } string directory = $"SchoolMgr/{fileTypeStr}"; FileManageClient client = new FileManageClient(ConfigurationManager.AppSettings["uploadUrl"]); var response = client.Upload(file.FileName, file.InputStream, ConfigurationManager.AppSettings["OssSubject"], directory, true); if (response.Code == Abhs.Service.Client.Results.FileManageStatusCode.Success) { response.FileUrl = $"/{directory}/{response.FileName}"; } return(Json(response)); } catch (Exception ex) { var msg = "服务器异常"; if (ex is AbhsException) { msg = ex.Message; } return(Json(new JsonSimpleResponse() { State = false, ErrorMsg = msg })); } }