예제 #1
0
        public async Task InvokeAsync(HttpContext httpContext)
        {
            httpContext.Response.ContentType = "application/json;charset=utf-8";
            //检查当前的请求方式
            if (httpContext.Request.Method.Equals(HttpMethods.Post))
            {
                //获取上传的文件
                var files = httpContext.Request.Form.Files;
                //验证是否上传文件
                if (files == null || files.Count() <= 0)
                {
                    myJsonResult.code = (int)MyJsonResultEnum.dataCode;
                    myJsonResult.msg  = "请上传文件";
                    await httpContext.Response.WriteAsync(myJsonResult.ToJson());

                    return;
                }
                var resPath = "";
                foreach (var file in files)
                {
                    //上传文件
                    var res = await _file.AddFileAsync(file);

                    //判断文件是否上传成功
                    if (!res.IsNullOrEmpty())
                    {
                        resPath += "," + res;
                    }
                }
                if (!resPath.IsNullOrEmpty())
                {
                    resPath = resPath.Substring(1, resPath.Length - 1);
                }
                fileResult.src         = resPath;
                fileResult.requestName = options.Value.RequestPathName;
                myJsonResult.rows      = fileResult;
            }
            else
            {
                myJsonResult.code = (int)MyJsonResultEnum.noFound;
                myJsonResult.msg  = "不支持的请求方式";
            }
            await httpContext.Response.WriteAsync(myJsonResult.ToJson());
        }