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文件的换行符全部丢失了, ...
随机推荐
- 14.6.3.2 Configuring Multiple Buffer Pool Instances 配置多个Buffer Poll 实例:
14.6.3.2 Configuring Multiple Buffer Pool Instances 配置多个Buffer Poll 实例: 对于系统有多个buffer pools 在多个字节范围, ...
- 执行计划之CONCATENATION
CREATE TABLE T_CONCAT (ID NUMBER, NAME VARCHAR2(30), TYPE VARCHAR2(30)); INSERT INTO T_CONCAT SELECT ...
- 悟透Javascript之 原型prototype
构造函数的Prototype上定义的方法确实可以通过对象直接调用,而且代码是共享的.我表示我不懂.太难理解了,艹.在Javascript中,prototype不但能让对象共享自己的财富,而且proto ...
- 转 @RenderBody()和@RenderSection()
强大的Razor引擎 一.Razor基础简介 Razor采用了cshtml后缀的文件名,截图如下: A. 版面布局 从图上看到,新的视图引擎已经没有了Site.Master这种MasterPage了, ...
- MySQL安装没有弹出配置向导
安装MySQL过程中一切都正常只是没有弹出MySQL配置向导对话框,即出现"launch the MySQL Instance Configuration Wizard" fini ...
- 【转】unity3d 各种优化综合
检测方式: 一,unity3d 渲染统计窗口 Game视窗的Stats去查看渲染统计的信息: 1.FPS fps其实就是 frames per second,也就是每一秒游戏执行的帧数,这个数值越 ...
- 小情人emacs的自动补全
前天打字的时候发现手指疼-..OTL-思考了一下可能是我近几个月以来一直在使用全部手敲代码不使用自动补全的"恶果"(当然我还是建议全部手敲的,至少可以感觉到强烈的屌丝满足感). 先 ...
- 最短路--Dijkstra算法 --HDU1790
//Dijkstra #include<iostream> #include<cstdio> #include<cstdlib> #include<cstri ...
- ASP.NET与SOAP协议使用记录
近期初次接手一个公司的管理系统开发任务,因为公司需要有Android,IOS客户端,又要求有PC端的网页客户端....对服务请求的要求自然也就落在了统一接口访问上了.... 使用ASP.NET的WEB ...
- kafka leader 服务器均衡。
Whenever a broker stops or crashes leadership for that broker's partitions transfers to other replic ...