zoukankan      html  css  js  c++  java
  • angular调用WCF服务,读取文件夹下图片显示列表,下载另存为图片

    读取文件夹下的文件

    public string ReadImagesPaths() 
            {
                string result = string.Empty;
                try 
                {
                    string path = System.IO.Directory.GetCurrentDirectory();
                    DirectoryInfo files = new DirectoryInfo(path+@"Images");
                    FileInfo[] fileinfo = files.GetFiles();
                    foreach (FileInfo file in fileinfo)
                    {
                        //result += files +@""+ file.Name + ";";
                        result += file.Name + ";";
                    }
                }
                catch(Exception ex)
                {
                    _log.Error(ex);
                }
                return result;
            }

    根据文件名下载图片并另存为:

    public Stream DownloadImage(string name)
            {
                string path = System.IO.Directory.GetCurrentDirectory();
                DirectoryInfo files = new DirectoryInfo(path + @"Images");
                FileInfo[] fileinfo = files.GetFiles();
                FileStream filecontent;
                Byte[] filebyte = new Byte[1];
                foreach (FileInfo file in fileinfo)
                {
                    if (file.Name == name)
                    {
                        string filepath = files + @"" + name;
                        filecontent = new FileStream(filepath,FileMode.Open);
                        filebyte = new Byte[filecontent.Length];
                        filecontent.Read(filebyte, 0, filebyte.Length);
                        filecontent.Close();
                    }
                }
                string encodedFileName = HttpUtility.UrlEncode(name);
                
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", string.Format("attachment;filename="{0}";filename*=utf-8'' {1}", encodedFileName, encodedFileName));
    
                return new MemoryStream(filebyte);

    前段代码:

    <button ng-click="imageDownload(item)">下载</button>
    this
    .downloadAccessory=function(fileId){ location.href=hostAddress+'MapScheme/ImageDownload?name='+name; };

    效果:

  • 相关阅读:
    sql
    Java后台通过传参得到对应的坐标值的方法
    运行第一个大型项目工程时出现的问题
    对sqlserver存储过程合游标循环中的一点心得
    (基于Java)算法之动态规划——矩阵连乘问题
    算法之线性时间选择(最坏情况下)
    算法之快速排序
    算法之合并排序
    算法之二分搜索法
    设计模式之装饰者模式
  • 原文地址:https://www.cnblogs.com/bobo-show/p/5125269.html
Copyright © 2011-2022 走看看