zoukankan      html  css  js  c++  java
  • wcf rest 服务用于安卓和ISO调用2-------文件上传

    接着上次写.......................

    这次利用wcf rest 上传文件.废话不多说.接着写代码.

    在上次的接口中添加上传文件方法UpLoad,代码如下:

    [WebInvoke(Method = "POST", UriTemplate = "UpLoad/{fileName}",
            ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
            [System.ComponentModel.Description("上传文件")]
            bool UpLoad(System.IO.Stream stream, string fileName);
    

    然后编写接口的方法实现:

     public bool UpLoad(Stream stream, string fileName)
            {
                try
                {
                    byte[] buffer = new byte[1024];
                    FileStream fs = new FileStream(@"d:	est" + fileName, FileMode.Create, FileAccess.Write);
                    int count = 0;
                    while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fs.Write(buffer, 0, count);
                    }
                    //清空缓冲区
                    fs.Flush();
                    //关闭流
                    fs.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("保存文件失败", ex);
                    return false;
                }
            }
    

      这里保存的路径,我就固定到d: est文件夹下面.然后在浏览器里面查看一下.

      接下来就编写客户端代码进行调用.接着上次的控制台应用程序写.代码如下图

     public static  void UploadFile()
            {
                Console.WriteLine("--------------上传文件----------------");
    
                HttpClient httpClient = new HttpClient();
                string uri = "http://localhost:31572/Service1.svc/UpLoad/";
                try
                {
                    string str;
                    do
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        Console.WriteLine("请输入上传文件路径:");
                        string filePath = Console.ReadLine();
                        if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
                        {
                            string fileName = filePath.Substring(filePath.LastIndexOf("\", StringComparison.Ordinal)+1);
                            Console.WriteLine("开始上传文件...");
                            Stream fs = new FileStream(filePath, FileMode.Open);
                            HttpContent content = new StreamContent(fs);
                            stopwatch.Start();
                            var response =  httpClient.PostAsync(uri + fileName, content);
                            response.Result.EnsureSuccessStatusCode();
                            Console.WriteLine("文件:{0},上传结果:{1}", fileName, response.Result.Content.ReadAsStringAsync().Result);
                            Console.WriteLine("上传文件所需时间:{0}", stopwatch.ElapsedMilliseconds);
                        }
                        else
                        {
                            Console.WriteLine("请输入正确的文件路径");
                        }
                        Console.WriteLine("y/n是否继续上传?");
                       str = Console.ReadLine();
                    } while (str=="y");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    Console.Read();
                }
            }
    

      最后效果如下图:

    下一篇就是soap和rest如何一起使用.

  • 相关阅读:
    VC++ 利用PDB和dump文件定位问题并进行调试
    MFC限制edit控件的字符输入长度
    VC++ 使用CreateProcess创建新进程
    正则表达式验证HTTP地址是否合法
    C++ _access和_waccess的使用方法
    最后一次谈 VirtualBox的安装方法
    解决/var/log下没有messages文件的问题?
    待续未完- 自己写后台内容管理程序的辅助内容
    php中的正则函数:正则匹配,正则替换,正则分割 所有的操作都不会影响原来的字符串.
    未完待续
  • 原文地址:https://www.cnblogs.com/yt-007/p/5176979.html
Copyright © 2011-2022 走看看