关键代码:
--上传的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. cout输出字符串指针

    先给出通过字符型指针输出字符串的示例代码,如下: #include <iostream>using std::cout;using std::endl; int main(){ const ...

  2. bzoj2007

    首先不难发现海拔高度只能为0或1 因为决策是单调的 不难发现最优决策一定是划分为海拔为0和1两块,不会出现01相间的情况 所以这很明显是一个最小割 由于n*n很大,我们必须要用平面图最小割转化为最短路 ...

  3. 深入浅出Node.js (附录D) - 搭建局域NPM仓库

    D.1 NPM仓库的安装 D.1.1 安装Erlang和CouchDB D.1.2 搭建NPM仓库 D.2 高阶应用 D.2.1 镜像仓库 D.2.2 私有模块应用 D.2.3 纯私有仓库 D.3 总 ...

  4. 【转】Eclipse中设置ButterKnife进行注解式开发步骤 -- 不错

    原文网址:http://www.bubuko.com/infodetail-974262.html 最近在进行Android注解式开发的学习,正在尝试用ButterKnife.ButterKnife的 ...

  5. (转载)获取当前运行的PHP版本信息

    (转载)http://www.clovery.org/get-the-php-version-information.html 获取PHP运行环境信息,可以使用下面的函数. <?php phpi ...

  6. iOS动态管理AutoLayout的约束NSLayoutConstraint

    除了使用Storyboard之外,也可以使用使用代码的方式,动态的向指定的UIView,添加约束. 例如有两个UILabel:someLabel,otherLabel 首先用代码实例化,两个控件 se ...

  7. Eclipse中svn图标不显示

    在菜单栏中:windows ->preferences->General->Appearance->Lable Decorations 勾选其中的 SVN 项,最后应用确认之后 ...

  8. second blog编程之美------控制cpu曲线

    先贴程序: 以前看过这个算法, 不过没什么印象,大概记得它利用while循环来控制cpu利用率 #include int main(int argc,char*argv[]) {         wh ...

  9. Spring MVC之messageConverters

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案.<mvc:annotation-dri ...

  10. Tomcat配置多个端口号或多个应用

    一.在Tomcat下配置一个应用服务(service)中,配置多个端口号. 即一个service配置多个端口,项目可以通过多个端口访问. 修改tomcat-home\conf下的server.xml, ...