Example #1
0
        public async Task <IActionResult> Post()
        {
            try
            {
                FileUploadResponse response         = new FileUploadResponse();
                DateTime           dtNow            = DateTime.UtcNow;
                string             currentDirectory = Directory.GetCurrentDirectory();
                var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), DefaultFormOptions.MultipartBoundaryLengthLimit);

                var reader  = new MultipartReader(boundary, HttpContext.Request.Body);
                var section = reader.ReadNextSectionAsync().Result;
                while (section != null)
                {
                    ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        var createdUid = HeaderUtilities.RemoveQuotes(contentDisposition.Name).Value;
                        if (string.IsNullOrEmpty(createdUid))
                        {
                            response.SetFail(BaseResponse.ErrorCodeEnum.File_CreatedUserIdIsNullOrEmpty);
                            return(Json(response));
                        }
                        var fileNameUpload = HeaderUtilities.RemoveQuotes(contentDisposition.FileName).Value;
                        if (!Validate(fileNameUpload))
                        {
                            response.SetFail(BaseResponse.ErrorCodeEnum.File_FileNameIsNullOrEmpty);
                            return(Json(response));
                        }
                        string fileName = dtNow.ToString("yyyyMMddHHmmss");
                        string filePath = Path.Combine(currentDirectory, ConfigSettingEnum.UploadPath.GetConfig(), createdUid, dtNow.Year.ToString(), dtNow.Month.ToString(), dtNow.Day.ToString());
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }
                        string extension    = Path.GetExtension(fileNameUpload);
                        string fullFileName = Path.Combine(filePath, string.Format("{0}{1}", fileName, extension));
                        lock (LockFile)
                        {
                            int i = 0;
                            while (System.IO.File.Exists(fullFileName))
                            {
                                i++;
                                fileName    += $"_{i}";
                                fullFileName = Path.Combine(filePath, string.Format("{0}{1}", fileName, extension));
                            }
                        }
                        using (var targetStream = System.IO.File.Create(fullFileName))
                        {
                            section.Body.CopyTo(targetStream);
                        }
                        string viewPath = $"{ConfigSettingEnum.CdnPath.GetConfig()}/{createdUid}/{dtNow.Year}/{dtNow.Month}/{dtNow.Day}";
                        response.Status   = true;
                        response.Name     = string.Format("{0}{1}", fileName, extension);
                        response.Path     = viewPath;
                        response.HostName = ConfigSettingEnum.UploadDomain.GetConfig();
                        Console.WriteLine("Response:" + response.FullUrl);
                        await _fileAppService.ImageAdd(new ImageAddModel
                        {
                            FilePath   = response.Path,
                            Extension  = extension,
                            FileName   = response.Name,
                            CreatedUid = createdUid
                        });

                        return(Json(response));
                    }
                    section = await reader.ReadNextSectionAsync();
                }
                return(NoContent());
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }