Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        public async Task InvokeAsync(HttpContext context)
        {
            if (options.RequestConstrainAsync != null)
            {
                if (!await options.RequestConstrainAsync(context))
                {
                    return;
                }
            }

            var request = context.Request;

            var dir  = "temps";
            var _dir = request.QueryString.GetValues("dir");

            if (!_dir.IsNullOrEmpty())
            {
                dir = _dir.FirstOrDefault();
            }

            var files = new List <UploadFileModel>(request.Files.Count);

            foreach (var key in request.Files.AllKeys)
            {
                using var memory = new MemoryStream();
                request.Files[key].InputStream.CopyTo(memory);
                files.Add(new()
                {
                    FileName   = request.Files[key].FileName,
                    FileBytes  = memory.ToArray(),
                    FileLength = request.Files[key].ContentLength,
                });
            }
            var model = new UploadModel
            {
                Files = files.ToArray(),
            };

            var paths = await model.SaveAsync(options, dir);

            var filePaths = paths.Select(_ => (_.Success, FilePath: _.Success ? _.FilePath.Replace(Path.DirectorySeparatorChar, '/') : _.FilePath));

            if (options.ReturnAbsolutePath)
            {
                var port = request.Url.IsDefaultPort ? null : $":{request.Url.Port}";
                filePaths = filePaths.Select(_ => (_.Success, _.Success ? $"{request.Url.Scheme}://{request.Url.Host}{port}/{_.FilePath}" : _.FilePath));
            }

            await options.SerializeToResponseAsync(context.Response, filePaths.Select(_ => _.FilePath));

            context.Response.End();
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var options = this.options.Value;

            if (options.RequestConstrainAsync != null)
            {
                if (!await options.RequestConstrainAsync(context))
                {
                    return;
                }
            }

            var request = context.Request;

            var dir = "temps";

            if (request.Query.TryGetValue("dir", out var _dir) && !_dir.IsNullOrEmpty())
            {
                dir = _dir.FirstOrDefault();
            }

            var files = new List <UploadFileModel>(request.Form.Files.Count);

            foreach (var item in request.Form.Files)
            {
                using var memory = new MemoryStream();
                item.OpenReadStream().CopyTo(memory);
                files.Add(new()
                {
                    FileName   = item.FileName,
                    FileBytes  = memory.ToArray(),
                    FileLength = item.Length,
                });
            }
            var model = new UploadModel
            {
                Files = files.ToArray(),
            };

            var paths = await model.SaveAsync(options, dir);

            var filePaths = paths.Select(_ => (_.Success, FilePath: _.Success ? _.FilePath.Replace(Path.DirectorySeparatorChar, '/') : _.FilePath));

            if (options.ReturnAbsolutePath)
            {
                filePaths = filePaths.Select(_ => (_.Success, _.Success ? $"{request.Scheme}://{request.Host}/{_.FilePath}" : _.FilePath));
            }

            await options.SerializeToResponseAsync(context.Response, filePaths.Select(_ => _.Success ? _.FilePath : string.Empty).ToArray());
        }