zoukankan      html  css  js  c++  java
  • 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("
    ") > -1)
        {
            string str2 = input.Substring(0, input.IndexOf("
    "));
            Regex regex = new Regex(@"(?<=Content-Type:)(.*?)(?=
    
    )");
            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) + "
    
    ".Length) + num2;
                byte[] serachFor = encoding.GetBytes("
    " + 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();
                            }
                        }
  • 相关阅读:
    IDEA 中直接连接远程服务器
    浙江大学软件学院2020年保研上机
    PAT甲级近五年题一览表
    浙江大学计算机与软件学院2021年考研复试上机
    浙江大学计算机与软件学院2019年保研上机
    PAT(甲级)2021年春季考试
    PAT(甲级)2020年冬季考试
    PAT(甲级)2020年秋季考试
    PAT(甲级)2020年春季考试
    PAT(甲级)2019年冬季考试
  • 原文地址:https://www.cnblogs.com/bobo-show/p/5511515.html
Copyright © 2011-2022 走看看