zoukankan      html  css  js  c++  java
  • WebClient UploadData UploadFile 用法

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="url">上传到目的服务的地址</param>
        /// <param name="url">上传到目的服务器的图片相对目录(如:aa/bb/cc/)</param>
        /// <param name="filename">扩展名</param>
        /// <param name="filename">上传文件的数据</param>
        /// <returns>图片地址</returns>
        private string upLoadPic(string url, string directory, string extension, byte[] data)
        {
            System.Net.WebClient myWebClient = new System.Net.WebClient();
            myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            myWebClient.QueryString["directory"] = directory;
            myWebClient.QueryString["extension"] = extension;
            byte[] responseArray = myWebClient.UploadData(url, "POST", data);
            return Encoding.GetEncoding("gb2312").GetString(responseArray);
        }

    使用方法:

    byte[] fileByte = new byte[files[0].ContentLength];
    System.IO.Stream fs = (System.IO.Stream)files[0].InputStream;
    fs.Read(fileByte, 0, files[0].ContentLength);
    fs.Close(); 
    string extension=Path.GetExtension(filename);
    string imgUrl = upLoadPic("目的服务器URL地址", "目录",extension,fileByte);


        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="url">上传到目的服务的地址</param>
        /// <param name="url">上传到目的服务器的图片相对目录(如:aa/bb/cc/)</param>
        /// <param name="filename">上传的文件名的完整路径</param>
        /// <returns>图片地址</returns>
        private string upLoadPic(string url, string directory, string fileName)
        {
            System.Net.WebClient myWebClient = new System.Net.WebClient();
            myWebClient.QueryString["directory"] = directory;
            byte[] responseArray = myWebClient.UploadFile(url, "POST", fileName);
            return Encoding.GetEncoding("gb2312").GetString(responseArray);
        }

    使用方法
    string fileName = @"C:\upload.txt";
    string imgUrl = upLoadPic("目的服务器URL地址", "目录",fileName);

  • 相关阅读:
    Redis
    Linux 软件安装
    Linux系统的目录和启动过程,Linux命令,权限控制
    虚拟机,Linux,VMware三种网络连接模式区别
    Spring Boot
    shiro和spring和springmvc的集成
    shiro
    Mybatis的逆向工程,MySQL8的数据库,8.0.11驱动的逆向工程的坑的解决方法
    jdk分析工具:jps和jstack
    如何使用jconsole(英文)
  • 原文地址:https://www.cnblogs.com/skykang/p/2048924.html
Copyright © 2011-2022 走看看