Exemplo n.º 1
0
        public async Task <IActionResult> UploadFileTemp()
        {
            var            data         = Request.Form.Files["file"];
            string         lastModified = Request.Form["lastModified"].ToString();
            var            total        = Convert.ToInt32(Request.Form["total"]);
            var            fileName     = Request.Form["fileName"];
            var            index        = Convert.ToInt32(Request.Form["index"]);
            MergeFileModel result       = new MergeFileModel();
            string         temporary    = Path.Combine(@"D:\tempFiles", lastModified);//临时保存分块的目录

            try
            {
                if (!Directory.Exists(temporary))
                {
                    Directory.CreateDirectory(temporary);
                }
                string filePath = Path.Combine(temporary, index.ToString());
                if (!Convert.IsDBNull(data))
                {
                    await Task.Run(() =>
                    {
                        using (var fs = new FileStream(filePath, FileMode.Create))
                        {
                            data.CopyTo(fs);
                        }
                    });
                }

                if (total == index)
                {
                    result = await FileMerge(lastModified, fileName);
                }
                result.FileNumber = index;
            }
            catch (TaskCanceledException ex)
            {
                logger.Error("Connection timed out and file upload failed. ", ex);
                return(Request.OK(new { msg = I18NEntity.GetString("GC_Common_Uploader_TimeOut_Message") }));
            }
            catch (ValidationException ex)
            {
                logger.Error("Validation file upload failed. ", ex);
                return(Request.OK(new { msg = I18NEntity.GetString("GC_Common_Uploader_Failed_Message"), ex.Status }));
            }
            catch (Exception ex)
            {
                logger.Error("Upload file to lcms server failed. ", ex);
            }

            return(Request.OK(result));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UploadSegmentFile()
        {   //==Form file Info ==
            var    formFile      = Request.Form.Files["file"];
            string correlationId = Request.Form["correlationId"];
            var    total         = Convert.ToInt32(Request.Form["totalCount"]);
            var    fileName      = Request.Form["fileName"];
            var    index         = Convert.ToInt32(Request.Form["index"]);
            string scopePath     = Request.Form["scopePath"];
            string category      = Request.Form["category"];

            var currentUserId        = HttpContext.CurrentUserId();
            var fileControlSetting   = _commonQueryService.GetFileControl();
            var newFileInfos         = new List <UploadFileResponseInfo>();
            FileResponseModel result = new FileResponseModel();

            logger.Info($"Begin to upload file. File count:{total}, current user:{HttpContext.CurrentUserName()}.");

            #region ==== Validate File Info =====
            if (index == 1)
            {
                UploadStatus validationResult = UploaderHelper.ValidationUploadFile(formFile.FileName, formFile.OpenReadStream(), fileControlSetting, true);
                if (validationResult != UploadStatus.ValidateSuccessful)
                {
                    logger.Warn($"Uploaded file is invalid. File name {formFile.Name}, size:{formFile.Length}, result:{validationResult}.");
                    return(Request.OK(new { msg = I18NEntity.GetString("GC_Common_Uploader_Failed_Message"), status = validationResult }));
                }
            }
            #endregion

            try
            {
                UploadFileRequestInfo info = new UploadFileRequestInfo()
                {
                    ScopePath     = !string.IsNullOrEmpty(scopePath) ? scopePath : ScopePath,
                    Category      = !string.IsNullOrEmpty(category) ? category : Category,
                    FileName      = fileName,
                    TotalCount    = total,
                    CorrelationId = !string.IsNullOrEmpty(correlationId) ? Guid.Parse(correlationId) : Guid.Empty,
                    Index         = index
                };

                if (total > 1)
                {
                    logger.Info($"Uploading Segment file {formFile.Name} to lcms.TotalCount:{info.TotalCount},current index:{info.Index}, CorrelationId:{info.CorrelationId}.");
                }
                else
                {
                    logger.Info($"Uploading file {formFile.Name} to lcms.");
                }

                UploadFileResponseInfo o = await _uploaderService.UploadFile(info, new Refit.StreamPart(formFile.OpenReadStream(), formFile.FileName));

                result.Info       = o;
                result.FileNumber = index;
                if (o.IsSucceed)
                {
                    //total file upload successfully
                    if (o.FileId != Guid.Empty)
                    {
                        logger.Info($"Uploading file {formFile.Name} to lcms succeed. File id:{o.FileId}.");
                        result.MergeResult = true;
                        o.Category         = Category;
                        result.ResponseFiles.Add(o);
                    }
                    else
                    {
                        result.MergeResult = false;
                    }
                }
                else
                {
                    logger.Warn($"Uploading file {formFile.Name} to lcms failed. File id:{o.FileId}, message:{o.ErrorMsg}.");
                    result.Msg = o.ErrorMsg;
                }
            }
            catch (TaskCanceledException ex)
            {
                logger.Error("Connection timed out and file upload failed. ", ex);
                return(Request.OK(new { msg = I18NEntity.GetString("GC_Common_Uploader_TimeOut_Message") }));
            }
            catch (ValidationException ex)
            {
                logger.Error("Validation file upload failed. ", ex);
                return(Request.OK(new { msg = I18NEntity.GetString("GC_Common_Uploader_Failed_Message"), ex.Status }));
            }
            catch (Exception ex)
            {
                logger.Error("Upload file to lcms server failed. ", ex);
            }

            return(Request.OK(result));
        }