WCF上传、下载、删除文件
关键代码:
--上传的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上传、下载、删除文件的更多相关文章
- java FTP 上传下载删除文件
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...
- java 通过sftp服务器上传下载删除文件
最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...
- 通过代码链接ftp上传下载删除文件
因为我的项目是Maven项目,首先要导入一个Maven库里的包:pom.xml <dependency> <groupId>com.jcraft</ ...
- Xshell5下利用sftp上传下载传输文件
sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH ...
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
- Struts2 文件上传,下载,删除
本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...
- SpringMVC ajax技术无刷新文件上传下载删除示例
参考 Spring MVC中上传文件实例 SpringMVC结合ajaxfileupload.js实现ajax无刷新文件上传 Spring MVC 文件上传下载 (FileOperateUtil.ja ...
- 使用C#WebClient类访问(上传/下载/删除/列出文件目录)由IIS搭建的http文件服务器
前言 为什么要写这边博文呢?其实,就是使用C#WebClient类访问由IIS搭建的http文件服务器的问题花了我足足两天的时间,因此,有必要写下自己所学到的,同时,也能让广大的博友学习学习一下. 本 ...
- 使用C#WebClient类访问(上传/下载/删除/列出文件目录)
在使用WebClient类之前,必须先引用System.Net命名空间,文件下载.上传与删除的都是使用异步编程,也可以使用同步编程, 这里以异步编程为例: 1)文件下载: static void Ma ...
- 使用ftp软件上传下载php文件时换行丢失bug
正 文: 在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了, ...
随机推荐
- lc面试准备:Remove Duplicates from Sorted List II
1 题目 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- Erlang安装简介
什么是 Erlang? Erlang 由 Ericsson (爱立信公司)开发,用于帮助开发管理许多电信项目的软件.Erlang 的第一个版本发布于 1986 年,1998 年发布了它的第一个开放源码 ...
- Linux Shell编程(12)——操作符
赋值变量赋值初始化或改变一个变量的值=通用的变量赋值操作符,可以用于数值和字符串的赋值 1 var=27 2 category=minerals # "="字符后面不能加 ...
- HDU 5933 ArcSoft's Office Rearrangement 【模拟】(2016年中国大学生程序设计竞赛(杭州))
ArcSoft's Office Rearrangement Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- DirectDraw
一.DirectDraw接口 DirectDraw接口图如下: 1.IUnknown:所有COM对象都必须从这个基本接口派生 2.IDirectDraw:这是开始使用DirectDraw时必须创建的主 ...
- stringstream 与空格 (大家讨论一下代码结果的原因)
#include <iostream> // std::cout, std::endl #include <iomanip> // std::setw #include < ...
- JavaScript高级程序设计16.pdf
第8章 BOM BOM的核心对象就是window,它表示浏览器的一个实例,在浏览器中window对象有双重角色,它既是JavaScript访问浏览器的一个接口,又是规定的Global对象,因此所有在全 ...
- HDU 3078 Network LCA
题意:n个点 m个询问,下面一行是n 个点的权值 再下面n-1行是双向的边 然后m个询问:k u v 若k==0,则把u点的权值改为v,否则回答u->v之间最短路经过点的权值中 第k大的值是多 ...
- linux下配置固定ip
今天在VM上装linux6.3,装的时候没有配置ip,虚拟机连接方式选的NAT方式,可以直接上网.我装这几次虚拟机系统只有这次虚拟机上去网了,挺爽.但是问题又出来了,就是我本机Windows远程不上虚 ...
- myeclipse svn
打开myeclipse的help---install from site 点击add弹出对话框 在输入框中输入对应内容 http://subclipse.tigris.org/update_1.10. ...