1. 关键代码:
  2. --上传的stream处理,转为bytep[]
  3. private void Parse(Stream stream, Encoding encoding)
  4. {
  5. this.Success = false;
  6. byte[] bytes = this.ToByteArray(stream);
  7. string input = encoding.GetString(bytes);
  8. if (input.IndexOf("\r\n") > -)
  9. {
  10. string str2 = input.Substring(, input.IndexOf("\r\n"));
  11. Regex regex = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)");
  12. Match match = regex.Match(input);
  13. Match match2 = new Regex("(?<=filename\\=\\\")(.*?)(?=\\\")").Match(input);
  14. if (match.Success && match2.Success)
  15. {
  16. this.ContentType = match.Value.Trim();
  17. this.Filename = match2.Value.Trim();
  18. int num2 = encoding.GetByteCount(this.Filename) - Encoding.ASCII.GetByteCount(this.Filename);
  19. int startIndex = ((match.Index + match.Length) + "\r\n\r\n".Length) + num2;
  20. byte[] serachFor = encoding.GetBytes("\r\n" + str2);
  21. int count = this.IndexOf(bytes, serachFor, startIndex) - startIndex;
  22. byte[] dst = new byte[count];
  23. Buffer.BlockCopy(bytes, startIndex, dst, , count);
  24. this.FileContents = dst;
  25. this.Success = true;
  26. }
  27. }
  28. }
  29. private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex)
  30. {
  31. int index = ;
  32. int num2 = Array.IndexOf<byte>(searchWithin, serachFor[], startIndex);
  33. if (num2 != -)
  34. {
  35. while ((num2 + index) < searchWithin.Length)
  36. {
  37. if (searchWithin[num2 + index] == serachFor[index])
  38. {
  39. index++;
  40. if (index == serachFor.Length)
  41. {
  42. return num2;
  43. }
  44. }
  45. else
  46. {
  47. num2 = Array.IndexOf<byte>(searchWithin, serachFor[], num2 + index);
  48. if (num2 == -)
  49. {
  50. return -;
  51. }
  52. index = ;
  53. }
  54. }
  55. }
  56. return -;
  57. }
  58. --上传文件
  59. //存储到指定文件夹下(byte[] p)
  60. string path = System.IO.Directory.GetCurrentDirectory() + @"\ResourceFiles\";
  61. FileStream fileStream = null;
  62. FileInfo fileInfo = new FileInfo(path + filename + filetype);
  63. fileStream = fileInfo.OpenWrite();
  64. fileStream.Write(p, , p.Length);
  65. fileStream.Close();
  66. --下载文件(前端调用:window.open('127.0.0.1:8086/' + 'ResourceDownload/RSRFileDownload?Id=' + data.ID);)
  67. public Stream DownloadRSRFile(Guid id)
  68. {
  69. //根据ID获取文件信息(存到数据库的信息)
  70. ResourceFile rsrFileInfo = QueryRSRFileByID(id);
  71. //获取当前路径
  72. string path = System.IO.Directory.GetCurrentDirectory();
  73. DirectoryInfo files = new DirectoryInfo(path + @"\ResourceFiles");
  74. //读取指定文件夹下的文件信息
  75. FileInfo[] fileinfo = files.GetFiles();
  76. FileStream filecontent;
  77. Byte[] filebyte = new Byte[];
  78. //根据ID获取的信息组合文件名
  79. string filename = rsrFileInfo.FileSaveName + rsrFileInfo.Type;
  80. foreach (FileInfo file in fileinfo)
  81. {
  82. if (file.Name == filename)
  83. {
  84. string filepath = files + @"\" + file;
  85. filecontent = new FileStream(filepath, FileMode.Open);
  86. filebyte = new Byte[filecontent.Length];
  87. filecontent.Read(filebyte, , filebyte.Length);
  88. filecontent.Close();
  89. }
  90. }
  91. string encodedFileName = HttpUtility.UrlEncode(rsrFileInfo.FileName);
  92.  
  93. WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
  94. WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", string.Format("attachment;filename=\"{0}\";filename*=utf-8'' {1}", encodedFileName, encodedFileName));
  95.  
  96. return new MemoryStream(filebyte);
  97. }
  98. --删除指定文件夹下的文件
  99. ResourceFile rsrFileInfo = QueryRSRFileByID(srcid);
  100. string path = System.IO.Directory.GetCurrentDirectory();
  101. DirectoryInfo files = new DirectoryInfo(path + @"\ResourceFiles");
  102. FileInfo[] fileinfo = files.GetFiles();
  103. Byte[] filebyte = new Byte[];
  104. string filename = rsrFileInfo.FileSaveName + rsrFileInfo.Type;
  105. foreach (FileInfo file in fileinfo)
  106. {
  107. if (file.Name == filename)
  108. {
  109. file.Delete();
  110. }
  111. }

WCF上传、下载、删除文件的更多相关文章

  1. java FTP 上传下载删除文件

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  2. java 通过sftp服务器上传下载删除文件

    最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...

  3. 通过代码链接ftp上传下载删除文件

    因为我的项目是Maven项目,首先要导入一个Maven库里的包:pom.xml <dependency>            <groupId>com.jcraft</ ...

  4. Xshell5下利用sftp上传下载传输文件

    sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH ...

  5. SpringMVC文件上传下载(单文件、多文件)

    前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...

  6. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

  7. SpringMVC ajax技术无刷新文件上传下载删除示例

    参考 Spring MVC中上传文件实例 SpringMVC结合ajaxfileupload.js实现ajax无刷新文件上传 Spring MVC 文件上传下载 (FileOperateUtil.ja ...

  8. 使用C#WebClient类访问(上传/下载/删除/列出文件目录)由IIS搭建的http文件服务器

    前言 为什么要写这边博文呢?其实,就是使用C#WebClient类访问由IIS搭建的http文件服务器的问题花了我足足两天的时间,因此,有必要写下自己所学到的,同时,也能让广大的博友学习学习一下. 本 ...

  9. 使用C#WebClient类访问(上传/下载/删除/列出文件目录)

    在使用WebClient类之前,必须先引用System.Net命名空间,文件下载.上传与删除的都是使用异步编程,也可以使用同步编程, 这里以异步编程为例: 1)文件下载: static void Ma ...

  10. 使用ftp软件上传下载php文件时换行丢失bug

    正 文:   在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了, ...

随机推荐

  1. 最好的JAVA IDE IntelliJ IDEA使用简介(一)—之界面元素

    打开IDEA,(当第一次打开的时候出现的是一个欢迎页面,随便创建一个project来进入到IDEA的主界面),主界面显示如下: 主界面由6个主要区域组成(图中红色数字标注的) 1.菜单和工具栏 2.导 ...

  2. Tcxtreelist动态控制列或行是否能够编辑

    procedure Tfrmaaa.grd1Editing(Sender: TObject; AColumn: TcxTreeListColumn; var Allow: Boolean);begin ...

  3. 【转】SVN linux命令及 windows相关操作(三)

    TortoiseSVN是windows下其中一个非常优秀的SVN客户端工具.通过使用它,我们可以可视化的管理我们的版本库.不过由于它只是一个客户端,所以它不能对版本库进行权限管理. TortoiseS ...

  4. 容斥原理应用(求1~r中有多少个数与n互素)

    问题:求1~r中有多少个数与n互素. 对于这个问题由容斥原理,我们有3种写法,其实效率差不多.分别是:dfs,队列数组,位运算. 先说说位运算吧: 用二进制1,0来表示第几个素因子是否被用到,如m=3 ...

  5. php中strlen和{}的效率对比

    很少有人知道{}用来判断字符串长度 今天试试 发现好像没有strlen快

  6. c# 字符串转化成声音 分类: C# 2014-09-24 12:20 316人阅读 评论(0) 收藏

    说明: (1)支持Window 7系统,但是xp系统智能朗读英文和数字: (2)添加引用 Interop.SpeechLib.dll; (3)使用时调用StringToVoice(str)即可. us ...

  7. Winform 中 DesignMode 返回值不正确的问题。

    本文转载:http://blog.csdn.net/sabty/article/details/5325260 以前也曾遇到这样的问题,不过影响不大也没有去详细了解.今天又重新遇到此问题,实在太不便. ...

  8. Oracle char 查询问题

    近期碰到一个问题,数据库有个字段设置类型是char(2),然后数据库保存的数据位1,当使用hibernate 查询时 我使用query.setString("permissionLevel& ...

  9. MFC编程小技巧——强制杀死进程

    在某些应用场合下,我们可能需要在启动A进程启动时关闭进程B.MFC下该如何做呢?以下是我项目中用到的代码: int KillProcess(DWORD Pid) { //打开进程得到进程句柄 HAND ...

  10. 使用jsdoc-toolkit来自动生成js api文档

    近来前端组小盆友开发的类库越来越多,很多情况下彼此不知道写了些什么方法,为了更好的合作提高工作效率,找了个比较好的api文档生成方法.使用jsdoc-toolkit来自动生成js api文档. 一.  ...