• WCF webHttpBinding协议上传接收文件


    一般情况下wcf用webHttpBinding协议最多的场景就是前后端Json交互,会比较轻量级。

    接收上传的文件也可以,不过要自己解析处理。

    前端HTML很简单:

            <input type="file" id="excel"/>
            <div class="btn btn-primary"  ng-click="upFile()">上传文件</div>

    前端JS也很简单:

            $scope.upFile = function () {
                var form = new FormData();
                var file = document.getElementById("excel").files[0];
                form.append('file', file);
                $http.post('/SvcWms/InDepot/UpFile', form).then(function (data) {
                    if(data.data.State === 1){
                        console.log('upload success');
                    }
                });
            };

    后台接口定义:

            [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
            DtoResponse<bool> UpFile(Stream file);

    接口实现:

        public class DtoResponse<T>
        {
            public int State { get; set; } = 1;
            public string Msg { get; set; }
            public T Data { get; set; }
        }
    
            public DtoResponse<bool> UpFile(Stream file)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    ms.Position = 0;
                    using (StreamReader sr = new StreamReader(ms))
                    {
                        int firstLineLen = Encoding.UTF8.GetBytes(sr.ReadLine()??"").Length;
                        var position = firstLineLen + 2;
                        var line = sr.ReadLine();
                        var filename = DateTime.Now.ToString("yyMMdd.HHmmss.");
    
                        if(!string.IsNullOrEmpty(line)) {
                            int idx = line.IndexOf("filename", StringComparison.CurrentCultureIgnoreCase);
                            filename += line.Substring(idx + 9).Replace(""", "");
                        }
    
                        while (line != null)
                        {
                            // 特别是第二行含文件名称,可能含中文,所以不能直接用line.Length
                            // 因为line.Length是字符个数,不是字节个数
                            position += Encoding.UTF8.GetBytes(line).Length + 2;
                            if (line == "")
                                break;
                            line = sr.ReadLine();
                        }
                        ms.Position = position;
                        ms.SetLength(ms.Length - (firstLineLen + 6));
    
                        var uploadStream = new MemoryStream();
                        ms.CopyTo(uploadStream);
                        uploadStream.Position = 0;
    
                        File.WriteAllBytes($"d:\{filename}", uploadStream.ToArray());
                    }
                }
                return new DtoResponse<bool>() {State = 1};
            }
  • 相关阅读:
    继承和多态
    访问限制
    返回函数
    类和实例
    requests
    函数的参数
    代码块的快速放置
    19进阶、基于TSP的直流电机控制设计
    18进阶、TLC语言
    17高级、Simulink代码生成技术详解
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/10575155.html
Copyright © 2020-2023  润新知