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") > -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();
                        }
                    }

 

转载于:https://www.cnblogs.com/bobo-show/p/5511515.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值