关键代码:
--上传的stream处理,转为bytep[]
private void Parse(Stream stream, Encoding encoding)
{
this.Success = false;
byte[] bytes = this.ToByteArray(stream);
string input = encoding.GetString(bytes);
if (input.IndexOf("\r\n") > -)
{
string str2 = input.Substring(, input.IndexOf("\r\n"));
Regex regex = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)");
Match match = regex.Match(input);
Match match2 = new Regex("(?<=filename\\=\\\")(.*?)(?=\\\")").Match(input);
if (match.Success && match2.Success)
{
this.ContentType = match.Value.Trim();
this.Filename = match2.Value.Trim();
int num2 = encoding.GetByteCount(this.Filename) - Encoding.ASCII.GetByteCount(this.Filename);
int startIndex = ((match.Index + match.Length) + "\r\n\r\n".Length) + num2;
byte[] serachFor = encoding.GetBytes("\r\n" + str2);
int count = this.IndexOf(bytes, serachFor, startIndex) - startIndex;
byte[] dst = new byte[count];
Buffer.BlockCopy(bytes, startIndex, dst, , count);
this.FileContents = dst;
this.Success = true;
}
}
}
private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex)
{
int index = ;
int num2 = Array.IndexOf<byte>(searchWithin, serachFor[], startIndex);
if (num2 != -)
{
while ((num2 + index) < searchWithin.Length)
{
if (searchWithin[num2 + index] == serachFor[index])
{
index++;
if (index == serachFor.Length)
{
return num2;
}
}
else
{
num2 = Array.IndexOf<byte>(searchWithin, serachFor[], num2 + index);
if (num2 == -)
{
return -;
}
index = ;
}
}
}
return -;
}
--上传文件
//存储到指定文件夹下(byte[] p)
string path = System.IO.Directory.GetCurrentDirectory() + @"\ResourceFiles\";
FileStream fileStream = null;
FileInfo fileInfo = new FileInfo(path + filename + filetype);
fileStream = fileInfo.OpenWrite();
fileStream.Write(p, , p.Length);
fileStream.Close();
--下载文件(前端调用:window.open('127.0.0.1:8086/' + 'ResourceDownload/RSRFileDownload?Id=' + data.ID);)
public Stream DownloadRSRFile(Guid id)
{
//根据ID获取文件信息(存到数据库的信息)
ResourceFile rsrFileInfo = QueryRSRFileByID(id);
//获取当前路径
string path = System.IO.Directory.GetCurrentDirectory();
DirectoryInfo files = new DirectoryInfo(path + @"\ResourceFiles");
//读取指定文件夹下的文件信息
FileInfo[] fileinfo = files.GetFiles();
FileStream filecontent;
Byte[] filebyte = new Byte[];
//根据ID获取的信息组合文件名
string filename = rsrFileInfo.FileSaveName + rsrFileInfo.Type;
foreach (FileInfo file in fileinfo)
{
if (file.Name == filename)
{
string filepath = files + @"\" + file;
filecontent = new FileStream(filepath, FileMode.Open);
filebyte = new Byte[filecontent.Length];
filecontent.Read(filebyte, , filebyte.Length);
filecontent.Close();
}
}
string encodedFileName = HttpUtility.UrlEncode(rsrFileInfo.FileName); 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);
}
--删除指定文件夹下的文件
ResourceFile rsrFileInfo = QueryRSRFileByID(srcid);
string path = System.IO.Directory.GetCurrentDirectory();
DirectoryInfo files = new DirectoryInfo(path + @"\ResourceFiles");
FileInfo[] fileinfo = files.GetFiles();
Byte[] filebyte = new Byte[];
string filename = rsrFileInfo.FileSaveName + rsrFileInfo.Type;
foreach (FileInfo file in fileinfo)
{
if (file.Name == filename)
{
file.Delete();
}
}

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. 居然还有WM_TIMECHANGE(只在用户手动改变系统时间时才会产生作用)

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  2. Quartz 有状态的JobDataMap

    Quartz,每次执行job,job永远是全新的对象,但是,如果job实现org.quartz.StatefulJob接口,而不是job接口. 此时JobDetail的JobDataMap将会共享一个 ...

  3. FILTER优化

    explain plan for select a.* from fxqd_list_20131115_new_100 a where (acct_no, oper_no, seqno, trans_ ...

  4. dpkg error

    在ubuntu乱搞,突然出现错误 dpkg: error: cannot read info directory: No such file or directory E: Sub-process / ...

  5. Xmanager连接CentOS的远程桌面

    本文主要介绍通过Xmanager连接CentOS远程桌面时,在CentOS系统上需要做的一些配置. 1. Xmanager简介 Xmanager是一个运行于 Windows平台上的高性能的X Serv ...

  6. unity3d Hair real time rendering 真实头发实时渲染

    先放上效果 惊现塞拉酱 算法是Weta Digital根据siggraph2003的论文加以改进,改进之前使用的是Kajiya and Kay’s 模型,它能量不守恒,也就是说不是基于物理的,不准确 ...

  7. Objective-c初始化和便利构造

    1.创建一个Student这个类 2.声明和实现 1).在Studnet.h文件中对属性和方法的声明 其中这个方法是带参初始化 而这个方法是便利构造.注意与上边的区别 2.在Studnet.m中实现 ...

  8. leetcode排列,求第k个排列

    stl 中的下一个排列在写一遍忘了 写个1个多小时,使用递归写的,错误就在我使用一个list保存当前剩下的数,然后利用k/(n-1)!的阶乘就是删除的数字,但进过观察, 比如 list={1,2,3} ...

  9. C++之友元函数

    1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 具体来说:为了使其他类的成员函数直接访问该类的私有变量 即:允许外面的类或函数去访问类的私有变量和保护变量,从而使两个类共享同 ...

  10. net user命令

    net user net user 用户名 net user 用户名 密码 /add net user 用户名 /del net localgroup administrators net local ...