//图片上传到阿里云 [AcceptVerbs(HttpVerbs.Post)] public JsonResult Upload(HttpPostedFileBase fileData, string tupianming) { if (fileData != null) { try { // 文件上传后的保存路径 var extName = Path.GetExtension(fileData.FileName); if (string.IsNullOrWhiteSpace(extName)) { return Json(new { Success = false, Message = "extName is null!" }); } var extNameToLower = extName.ToLower(); var accessId = System.Configuration.ConfigurationManager.AppSettings["ossKeyId"]; var accessKey = System.Configuration.ConfigurationManager.AppSettings["ossKeySectet"]; var bucketName = "xiaoyaodijun"; //System.Configuration.ConfigurationManager.AppSettings["bucketName"]; var endpoint = "http://oss-cn-qingdao.aliyuncs.com "; var extenname = ".oss-cn-qingdao.aliyuncs.com/"; //图片的存储路径和名字 var fileName = "xiaoyao/124" + extNameToLower; OssClient client = new OssClient(endpoint, accessId, accessKey); System.IO.Stream inputStream = fileData.InputStream; ObjectMetadata objectMetadata = new ObjectMetadata(); client.PutObject(bucketName, fileName, inputStream, objectMetadata); //返回图片路径 AccessControlList bucketAcl = client.GetBucketAcl(bucketName); var result = ((!bucketAcl.Grants.Any()) ? client.GeneratePresignedUri(bucketName, fileName, System.DateTime.Now.AddMinutes(5.0)).AbsoluteUri : string.Format("http://{0}{1}{2}", bucketName, extenname, fileName)); inputStream.Dispose(); } catch (Exception ex) { return Json(new { Success = false, Message = ex.Message }); } } else { return Json(new { Success = false, Message = "请选择要上传的文件!" }); } return null; }
时间: 2024-10-29 19:08:30