
- [HttpPost]
- public dynamic UploadSaveOSS()
- {
- try
- {
- //int timeStamp = System.Web.HttpContext.Current.Request.QueryString["ts"].ToInt32(0);
- //超出有效期
- //if (Math.Abs((int)DateTime.Now.ToUnixTimeStamp() - timeStamp) > Const.PcAccessValidSeconds)
- //{
- // throw new HttpResponseException(HttpStatusCode.Unauthorized);
- //}
- long userId = 0;
- //string token = HttpContext.Current.Request.Form["token"];
- //if (string.IsNullOrEmpty(token))
- //{
- // return CommonService.GetErrorResult(Const.ParamErrorTip);
- //}
- //JwtBody jwtBody = CommonHelper.GetUserInfoFromAuth2<JwtBody>("Bearer " + token);
- //if (jwtBody.nameid < 1)
- //{
- // return CommonService.GetErrorResult(Const.FailRetryTip);
- //}
- //userId = jwtBody.nameid;
- HttpFileCollection files = HttpContext.Current.Request.Files;
- HttpPostedFile postedFile = files["file"];
- if (postedFile.ContentLength > CommonHelper.ConvertToByte(Const.UploadMaxLength))
- {
- return CommonService.GetErrorResult("文件大小不能超过" + Const.UploadMaxLength);
- }
- int maxlength = 5242880;//5M=5*1024*1024
- string size = HttpContext.Current.Request.Form["size"];
- if (!string.IsNullOrEmpty(size))
- {
- maxlength = CommonHelper.ConvertToByte(size);
- }
- if (postedFile.ContentLength > maxlength)
- {
- return CommonService.GetErrorResult("文件大小不能超过" + size);
- }
- else
- {
- int utp = Convert.ToInt32(HttpContext.Current.Request.Form["utp"]);
- string fileName, fileExtension;
- fileName = System.IO.Path.GetFileName(postedFile.FileName);
- if (fileName != "")
- {
- string newpath = System.DateTime.Now.ToString("yyyyMMdd");
- string savepath = Const.WebFileFolderName + "/temp/" + newpath + "/";
- if (utp == (sbyte)UploadType.KnowledgePic)
- {
- savepath = Const.WebFileFolderName + "/KnowledgePic/";
- }
- fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
- string filename = System.DateTime.Now.ToString("yyyyMMddhhmmssfff") + fileExtension;
-
- OssClient client = new OssClient(Const.EndPoint, Const.AccessKeyId, Const.AccessKeySecret);
- string key = savepath + filename;
- client.PutObject(Const.WebFileBucketName, key, postedFile.InputStream);
- //获取访问url
- // 设置URL过期时间为50年,图片最好设置为1天过期,源文件1小时过期,图片获取的时候循环判断是否过期,源文件下载的时候判断过期
- DateTime expiration = DateTime.Now.AddYears(50);
- Uri uri = client.GeneratePresignedUri(Const.WebFileBucketName, key, expiration);
- string path = uri.ToString();
- int affect = 1;
-
- if (affect > 0)
- {
- return CommonService.GetResult("", ErrorCodeType.Success, new
- {
- path = path,
- name = fileName
- });
- }
- else
- {
- return CommonService.GetErrorResult("上传失败");
- }
- }
- else
- {
- return CommonService.GetErrorResult("上传文件为空");
- }
- }
- }
- catch
- {
- return CommonService.GetErrorResult(Const.FailRetryTip);
- }
- }
评价
排名
11
文章
201
粉丝
10
评论
13
EF连接MySQL连接MySQL数据库操作中文数据乱码解决方法
剑轩 :
准确点应该是ef连接mysql
Layui插件上传图片具体步奏以及获取图片地址
风清月 : 厉害老!
Action与Func的用法-----委托
剑轩 : 可以可以
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术