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};
            }
  • 相关阅读:
    Linq 入门 顺带 Func与Action
    关于asp.net 的一些好资料地址 , 防止丢失!
    Sql日期时间格式转换 备用
    自己动手写 ASP.NET MVC 分页 part1
    怎么做好一个美食排行榜的用户投票功能?
    「要买车网」免费获取汽车电商要买车网购车优惠券
    MVC Ajax Form & Ajax Valida(笔记)
    C# 序列化高级用法
    我与葡萄城的故事
    生成分布式随机ID
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/10575155.html
Copyright © 2011-2022 走看看