文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>WebClient UploadData UploadFile 用法

WebClient UploadData UploadFile 用法

时间:2011-05-17  来源:skykang

    /// <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);

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载