zoukankan      html  css  js  c++  java
  • 上传和下载文件

    1. 上传文件 
    
     FileStream fsRead = new FileStream(fileurl, FileMode.Open);
                int fsLen = (int)fsRead.Length;
                byte[] heByte = new byte[fsLen];
                fsRead.Read(heByte, 0, heByte.Length);
    
                long length = fsRead.Length;
                string filename = fsRead.Name.Substring(fsRead.Name.LastIndexOf("\") + 1);
                fsRead.Close();
    
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http:.../cunnar/upload?cId=" + cId + "&fileId=" + contractid + "&fileLength=" + length + "&fileName=" + filename);
                    request.CookieContainer = new CookieContainer();
                    request.Method = "POST";
                    request.ContentType = "application/octet-stream";
                    request.ContentLength = heByte.Length;
                    request.GetRequestStream().Write(heByte, 0, heByte.Length);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    Stream stream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(stream);
                    string result = sr.ReadToEnd();
    
                    jsonCode = Newtonsoft.Json.JsonConvert.DeserializeObject<ReturnJsonCode>(result);
                }
                catch (Exception ex)
                {
                    jsonCode.code = -1;
                    jsonCode.msg = "上传失败!";
                }
    
    
    2. 下载文件(直接打开pdf文件的数据流)
    public Stream DownloadFile(string cId, string cFileId)
        {
            ReturnJsonCode jsonCode=new ReturnJsonCode();
            try
            {
                string url = "http://.../cunnar/download?cId=" + cId +
                         "&cFileId=" + cFileId;
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = "application/pdf";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                return stream;
            }
            catch (Exception ex)
            {return null;
            }
    
    
      调用下载文件方法
            Stream fs = cert.DownloadFile("4960332", "1263932");
            MemoryStream ms = new MemoryStream();
            fs.CopyTo(ms);
            Response.ClearContent();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "inline;FileName=out.pdf");
            Response.BinaryWrite(ms.ToArray());
  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/sophiel/p/7008450.html
Copyright © 2011-2022 走看看