Beispiel #1
0
        /// <summary>
        ///  下载文件超过1.8GB时,通过HttpWebRequest的方式下载文件
        /// </summary>
        /// <param name="filename">Download File Name</param>
        /// <param name="fileId">Download File ID</param>
        /// <returns></returns>
        private FileResult HttpWebDownFile(UploadFileResponseInfo FileInfo)
        {
            FileResult result   = null;
            string     fileId   = FileInfo.FileId.ToString();
            string     fileName = FileInfo.FileName;

            logger.Info($"HttpWebDownFile: Bengin download file , file id:[{fileId}]");
            try
            {
                string LCMSHost = _configuration["LCMSHost"].TrimEnd('/');

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create($"{LCMSHost}/api/fileapi/Download?fileId={fileId}");
                request.Timeout = 30 * 60 * 1000;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string contentType = "application/x-msdownload";
                var    provider    = new FileExtensionContentTypeProvider();
                if (provider.Mappings.ContainsKey(FileInfo.ExtensionName))
                {
                    contentType = provider.Mappings[FileInfo.ExtensionName];
                }
                Response.ContentLength = FileInfo.FileSize;
                result = File(response.GetResponseStream(), contentType, fileName);

                logger.Info($"HttpWebDownFile: End download stream , {DateTime.Now} {fileName}");
            }
            catch (Exception ex)
            {
                logger.Error($"HttpWebDownFile: Download file error:{ex.ToString()}");
            }
            return(result);
        }
Beispiel #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));
        }