zoukankan      html  css  js  c++  java
  • 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};
            }
  • 相关阅读:
    linux 短信收发
    sama5d3 环境检测 adc测试
    【Codeforces 723C】Polycarp at the Radio 贪心
    【Codeforces 723B】Text Document Analysis 模拟
    【USACO 2.2】Preface Numbering (找规律)
    【Codeforces 722C】Destroying Array (数据结构、set)
    【USACO 2.1】Hamming Codes
    【USACO 2.1】Healthy Holsteins
    【USACO 2.1】Sorting A Three-Valued Sequence
    【USACO 2.1】Ordered Fractions
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/10575155.html
Copyright © 2011-2022 走看看