单个文件上传至服务器
#region 单个文件上传至服务器
/// <summary>
/// 单个文件上传至服务器
/// </summary>
/// <param name="uriAddress">接收文件资源的URI, 例如: http://xxxx/Upload.aspx?UID=11111</param>
/// <param name="filePath">要发送的资源文件, 例如: @"D:workspaceWebService 相关.doc</param>
/// <returns>返回文件保存的相对路径, 例如: "upload/xxxxx.jpg" 或者出错返回 ""</returns>
public static string UploadFile(string uriAddress, string filePath)
{
//利用 WebClient
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
byte[] responseArray = webClient.UploadFile(uriAddress, "POST", filePath);
string savePath = System.Text.Encoding.ASCII.GetString(responseArray);
return savePath;
}
catch (Exception)
{
return "";
}
}
#endregion