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());
  • 相关阅读:
    02、Rendering a Triangle
    [转]Unity性能优化之Draw Call
    [转]Directx11 3D空间坐标系认识
    设置让EditPlus不产生BAK文件
    深度优先搜索与广度优先搜索对比
    python多重继承新算法C3
    php的垃圾回收机制
    python脚本自动发邮件功能
    python的keyword模块
    EditPlus如何设置——自动换行
  • 原文地址:https://www.cnblogs.com/sophiel/p/7008450.html
Copyright © 2011-2022 走看看