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());
  • 相关阅读:
    treeview 递归
    循环递归遍历XML文档或按某要求遍历XML文档
    SQL Server、IIS和 ASP.NET安全配置
    C# USING 语句块
    C# 基础语法
    不安装oracle 客户端连接oracle DDTeck连接语法
    Java中堆和栈的区别
    C# 采用OLDB方式连接EXCEL
    EXCEL 列宽
    C# 问号用法
  • 原文地址:https://www.cnblogs.com/sophiel/p/7008450.html
Copyright © 2011-2022 走看看