关键代码:
--上传的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") > -1)
{
string str2 = input.Substring(0, 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, 0, count);
this.FileContents = dst;
this.Success = true;
}
}
}
private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex)
{
int index = 0;
int num2 = Array.IndexOf<byte>(searchWithin, serachFor[0], startIndex);
if (num2 != -1)
{
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[0], num2 + index);
if (num2 == -1)
{
return -1;
}
index = 0;
}
}
}
return -1;
}
--上传文件
//存储到指定文件夹下(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, 0, 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[1];
//根据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, 0, 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[1];
string filename = rsrFileInfo.FileSaveName + rsrFileInfo.Type;
foreach (FileInfo file in fileinfo)
{
if (file.Name == filename)
{
file.Delete();
}
}