zoukankan      html  css  js  c++  java
  • 在HTML中使用WCF RESTful上传文件

    说明

    在HTML中上传文件时会为文件内容加入一头一尾,如下所示:

    -----------------------8cc0b8cfcfd5ed2
    Content-Disposition: form-data; name="file"; filename="item3.xml"
    Content-Type: application/octet-stream
    
    这里是真正的文件内容
    -----------------------8cc0b8cfcfd5ed2--


    因此服务端接收后要手动对其作解析。



    代码

    网页端

    <!DOCTYPE HTML>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
        <meta http-equiv="pragma" content="no-cache" />
        <title>测试页</title>
    </head>
    
    <body>
        文件上传页
            <form action="http://localhost:4513/IVisitorRestService/Upload/utf-8" method="POST" enctype="multipart/form-data">
                <input type="file" />
                <input type="submit" />
            </form>
    </body>
    </html>



    服务端

    契约

    /// <summary>
    /// 使用HTML上传文件
    /// </summary>
    /// <param name="file">文件流</param>
    /// <param name="encodingName">HTML的文字编码名</param>
    [Custom(AllowAnonymousForOperation = true)]
    [WebInvoke(Method = "POST", UriTemplate = "Upload/{encodingName}")]
    void Upload(Stream file, string encodingName);


    实现

    public void Upload(Stream file, string encodingName)
    {
        using (var ms = new MemoryStream())
        {
            file.CopyTo(ms);
            ms.Position = 0;
    
            var encoding = Encoding.GetEncoding(encodingName);
            var reader = new StreamReader(ms, encoding);
            var headerLength = 0L;
    
            //读取第一行
            var firstLine = reader.ReadLine();
            //计算偏移(字符串长度+回车换行2个字符)
            headerLength += encoding.GetBytes(firstLine).LongLength + 2;
    
            //读取第二行
            var secondLine = reader.ReadLine();
            //计算偏移(字符串长度+回车换行2个字符)
            headerLength += encoding.GetBytes(secondLine).LongLength + 2;
            //解析文件名
            var fileName = new System.Text.RegularExpressions.Regex("filename=\"(?<fn>.*)\"").Match(secondLine).Groups["fn"].Value;
    
            //一直读到空行为止
            while (true)
            {
                //读取一行
                var line = reader.ReadLine();
                //若到头,则直接返回
                if (line == null)
                    break;
                //若未到头,则计算偏移(字符串长度+回车换行2个字符)
                headerLength += encoding.GetBytes(line).LongLength + 2;
                if (line == "")
                    break;
            }
    
            //设置偏移,以开始读取文件内容
            ms.Position = headerLength;
            ////减去末尾的字符串:“\r\n--\r\n”
            ms.SetLength(ms.Length - encoding.GetBytes(firstLine).LongLength - 3 * 2);
    
            using (var fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create))
            {
                ms.CopyTo(fileToupload);
                fileToupload.Close();
                fileToupload.Dispose();
            }
        }
    }


  • 相关阅读:
    幸福婚姻的八个公式
    中国移动增值服务的现状及趋势
    移动通信与互联网融合已成为趋势
    项目管理入门
    4GMF论坛主席卢伟谈4G全球发展概况
    吴刚实践总结手机网游十大金科玉律
    项目管理五大过程组在通信工程中的运用实例
    报告称近半WAP用户低学历 学生工人上网最多
    手机杂志:成长的烦恼
    3G门户网
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3017473.html
Copyright © 2011-2022 走看看