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