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());
  • 相关阅读:
    即时通讯之环信视频语音实时通话与单聊和群聊实现
    检测.net framework 版本
    Abp(.NetCore)开发与发布过程3-部署Ubuntu站点
    Abp(.NetCore)开发与发布过程2
    Abp(.NetCore)开发与发布过程
    Redis实战与 Session缓存
    Redis 安装与初体验
    [转]gluPerspective函数
    [转]gluLookAt 函数详解
    [转]unity3D游戏开发之GUI
  • 原文地址:https://www.cnblogs.com/sophiel/p/7008450.html
Copyright © 2011-2022 走看看