예제 #1
0
        public async Task <HttpResponseMessage> Get(Guid fileId, [FromServices] GetFileHandler fh)
        {
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

            var(stream, file) = await fh.Get(fileId);

            result.Content = new StreamContent(stream);
            result.Content.Headers.ContentType        = new MediaTypeHeaderValue(file.ContentType);
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = file.FileName
            };
            return(result);
        }
 public static IApplicationBuilder MapGetFile(this IApplicationBuilder app,
                                              IFileserverConfig <HttpRequest> config)
 {
     app.MapWhen(c => HttpMethod.Get.Method.Equals(c.Request.Method, StringComparison.OrdinalIgnoreCase),
                 builder =>
     {
         builder.Run(async context =>
         {
             var handler = new GetFileHandler(
                 builder.ApplicationServices.GetService(typeof(ILoggerFactory)) as ILoggerFactory,
                 config.GetFilePipeline());
             await handler.Invoke(context);
         });
     });
     return(app);
 }
예제 #3
0
        /// <summary>
        /// 文件处理模块
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("Pragma", "no-cache");
            context.Response.AddHeader("Cache-Control", "private, no-cache");
            context.Response.Charset         = "utf-8";
            context.Response.ContentEncoding = Encoding.GetEncoding("utf-8");
            Handler action = null;

            var fun = context.GetStringFromParameters("fun");

            switch (fun)
            {
            case "001":
                action = new UploadHandler(context, new UploadConfig
                {
                    AllowExtensions = Config.GetStringList("imageAllowFiles"),
                    PathFormat      = Config.GetString("imagePathFormat"),
                    SizeLimit       = Config.GetInt("imageMaxSize"),
                    UploadFieldName = Config.GetString("imageFieldName"),
                    PreUrl          = Config.GetString("imageUrlPrefix"),
                    Others          = context.GetStringFromParameters("others")
                });
                break;

            //获取文件记录
            case "002":
                action = new GetFileHandler(context);
                break;

            //接收文件
            case "003":
                action = new UploadPartHandler(context, new UploadConfig
                {
                    AllowExtensions = Config.GetStringList("imageAllowFiles"),
                    PathFormat      = Config.GetString("imagePathFormat"),
                    SizeLimit       = Config.GetInt("imageMaxSize"),
                    UploadFieldName = Config.GetString("imageFieldName"),
                    PreUrl          = Config.GetString("imageUrlPrefix")
                });
                break;

            //通知文件传输结束
            case "004":
                action = new FinishuploadHandler(context, new UploadConfig
                {
                    AllowExtensions = Config.GetStringList("imageAllowFiles"),
                    PathFormat      = Config.GetString("imagePathFormat"),
                    SizeLimit       = Config.GetInt("imageMaxSize"),
                    UploadFieldName = Config.GetString("imageFieldName"),
                    PreUrl          = Config.GetString("imageUrlPrefix")
                });
                break;

            //测试通信
            default:
                var result = new UploadResult {
                    State = UploadState.Success, Url = "测试通信"
                };
                var returnValue = JsonConvert.SerializeObject(result);
                context.Response.Write(returnValue);
                context.Response.End();
                break;
            }
            action?.Process();
        }
예제 #4
0
            /// <summary>
            /// Download a file from the server.
            /// </summary>
            /// <param name='fileName'>
            /// File name of the file (full path)
            /// </param>
            /// <param name='handler'>
            /// Callback handler that is invoked for each chunk of the file received
            /// </param>
            /// <param name='parameter'>
            /// User provided parameter that is passed to the callback handler
            /// </param>
            public void GetFile(string fileName, GetFileHandler handler, object parameter)
            {
                int error;

                GetFileCallback getFileCallback = new GetFileCallback(handler, parameter);

                GCHandle handle = GCHandle.Alloc(getFileCallback);

                IedConnection_getFile(connection, out error, fileName, new InternalIedClientGetFileHandler(iedClientGetFileHandler),
                    GCHandle.ToIntPtr(handle));

                if (error != 0)
                    throw new IedConnectionException("Error reading file", error);

                handle.Free();
            }
예제 #5
0
 public GetFileCallback(GetFileHandler handler, object parameter)
 {
     this.handler = handler;
     this.parameter = parameter;
 }
예제 #6
0
        public async Task <IActionResult> Get(Guid fileId, [FromServices] GetFileHandler fh)
        {
            var(stream, file) = await fh.Get(fileId);

            return(File(stream, file.ContentType, file.FileName));
        }