tnblog
首页
视频
资源
登录

文件上传

3560人阅读 2020/3/31 15:39 总访问:415056 评论:0 收藏:0 手机
分类: ASP.NET
[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);
            }
        }


评价
当你知道迷惑时,并不可怜,当你不知道迷惑时,才是最可怜的。
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术