コード例 #1
0
        public async Task <MessageModel <UploadFileModel> > DeleteThisFile(int?fileId)
        {
            var             data         = new MessageModel <UploadFileModel>();
            UploadFileModel upfileModels = new UploadFileModel();

            if (!fileId.HasValue)
            {
                data.msg = "没参数";
                return(data);
            }

            FileCenter tfile = await _fileCenterServices.QueryById(fileId.Value);

            if (tfile != null && tfile.ExterndAtt == SystemConst.FILE_EXTERNARR_THUMBONTHIS)
            {
                //检测一下,如果是主图,要重新生成主图和缩略图信息
                //下载下来这个批次中排首的图,重新生成缩略图
                FileCenter nextfile = await _fileCenterServices.GetFirstOneExceptMe(tfile.BatchId, tfile.Id);

                //更新这个图为主图
                Stream s = AliYunOss.Instance.GetFileFromAliOss(nextfile.OssKey);
                if (nextfile != null)
                {
                    string filethumbName = IdCreatorHelper.CreateIdNoTimestrap(SystemConst.PREF_THUMB_NAME_PUB_TREE, 6) + ".jpg";
                    string AliossFolder  = DateTime.Now.ToString("yyyyMMddHH");
                    string newthumbUrl   = await CreateThumb(s, nextfile.BatchId, AliossFolder + "/" + filethumbName);

                    nextfile.ExterndAtt = SystemConst.FILE_EXTERNARR_THUMBONTHIS;
                    //更新这个批次的缩略图信息

                    bool thumbok = await _fileCenterServices.UpdateThisFileUrlByBatchIdAndSeq(nextfile.BatchId, 0, newthumbUrl);

                    bool isOk = await _fileCenterServices.Update(nextfile);

                    upfileModels.MainPic      = nextfile.Url;
                    upfileModels.ThumbnailUrl = newthumbUrl;
                    data.response             = upfileModels;
                }
            }
            bool isOK = await _fileCenterServices.DeleteById(fileId.Value);

            data.msg = isOK ? "删除成功" : "删除失败";
            return(data);
        }
コード例 #2
0
        public async Task <MessageModel <UploadFileModel> > PushPic()
        {
            var             data         = new MessageModel <UploadFileModel>();
            int             currentIndex = 1;
            UploadFileModel upfileModels = new UploadFileModel();
            string          batchId      = Request.Form["batchId"];

            //先检查有没有batchid,如果有,说明已开始存入过,那么不需要new batchid
            if (string.IsNullOrEmpty(batchId))
            {
                batchId = IdCreatorHelper.CreateId(SystemConst.PREF_BATCHID_FOR_PUB_TREE);
            }
            else
            {
                //找到此batchid 已累计增加的index ,用缓存缓住
                string currindex = _cacheHelper.Get <string>(batchId);
                currentIndex = int.Parse(string.IsNullOrEmpty(currindex)?"1":currindex);
            }

            IFormFileCollection cols = Request.Form.Files;

            if (cols == null || cols.Count == 0)
            {
                data.msg = "没有上传文件";
                return(data);
            }
            //上传到oss
            string            AliossFolder = DateTime.Now.ToString("yyyyMMddHH");
            List <FileCenter> dbfiles      = new List <FileCenter>();

            upfileModels.BatchId = batchId;
            List <IFormFile> postFiles = cols.ToList();

            for (int i = 0; i < postFiles.Count; i++)
            {
                var tfile = postFiles[i];

                string fileName = AliossFolder + "/" + IdCreatorHelper.CreateIdNoTimestrap(SystemConst.PREF_ALIOSS_FILENAME_FOR_TREE, 6) + ".jpg";
                AliYunOss.Instance.PutFileToOss(tfile.OpenReadStream(), fileName);
                FileCenter f = new FileCenter();
                if (currentIndex == 1)//默认第一个设置缩略图,设为主图
                {
                    string     filethumbName = IdCreatorHelper.CreateIdNoTimestrap(SystemConst.PREF_THUMB_NAME_PUB_TREE, 6) + ".jpg";
                    FileCenter thumbfile     = await  CreateThumb(tfile, batchId, AliossFolder + "/" + filethumbName);

                    dbfiles.Add(thumbfile);
                    upfileModels.ThumbnailUrl = thumbfile.Url;
                    upfileModels.MainPic      = AliYunOssConfig.Endpoint + "/" + fileName; //第一个默认主图
                    f.ExterndAtt = SystemConst.FILE_EXTERNARR_THUMBONTHIS;                 //设置浮标,标明 缩略图建立在这个图上
                }

                f.BatchId    = batchId;
                f.BatchSeq   = currentIndex;
                f.CreateTime = DateTime.Now;
                f.FileName   = tfile.FileName;
                f.FileSize   = tfile.Length;
                f.Status     = 0;
                f.OssKey     = fileName;
                f.Url        = AliYunOssConfig.Endpoint + "/" + fileName;
                dbfiles.Add(f);
                currentIndex++;
            }
            _cacheHelper.Set <string>(batchId, currentIndex.ToString(), DateTime.Now.AddHours(1));
            try
            {
                bool isok = await _fileCenterServices.AddIsIdentity(dbfiles);

                dbfiles.ForEach(u => upfileModels.files.Add(u.FileName, u.Id));
            }
            catch (Exception ex)
            {
                string e = ex.ToString();
            }
            //返回这一批次ID,返回已上传的ID

            data.response = upfileModels;
            return(data);
        }