好库网 好库网首页 | 我的好库
好饱的专栏

C#上传文件到WEB服务器

发布者:好饱
发布日期:2012/8/5 22:12:32   更新日期:2012/8/5 22:12:32
阅读次数:10773
评分:4.80
介绍:我们可以使用WebClient和HttpWebRequest上传文件。
正文:

 

一、使用WebClient

WebClient类提供了UploadFile()和UploadData()方法。两者不同之处在于UploadFile()上传指定文件名的文件。

UploadData()上传二进制数据,数据以字节数组存在,例如:

 

UploadFile例子

 

WebClient  Client = new WebClient();
Client.UploadFile("http://www.csharpfriends.com/Members/index.aspx", 
     "c:\wesiteFiles\newfile.aspx");

UploadData例子

 

byte [] image;

//这里省略将图像数据读入byte数组的代码。
//.....
WebClient  Client = new WebClient();
client.UploadData("http://www.csharpfriends.com/Members/images/logocc.jpg", image);

 

 

 

二、使用HttpWebRequest

 

 

private void UploadFilesToRemoteUrl(string url, string[] files, string logpath)
{
  long length = 0;
  string boundary = "----------------------------" +
  DateTime.Now.Ticks.ToString("x");

  HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
  httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
  httpWebRequest2.Method = "POST";
  httpWebRequest2.KeepAlive = true;

  httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;

  Stream memStream = new System.IO.MemoryStream();

  byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

  memStream.Write(boundarybytes,0,boundarybytes.Leng th);
  length += boundarybytes.Length;

  string headerTemplate = "Content-Disposition: form-data; name=\"{0}\";
  filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

  for(int i=0;i<files.Length;i++)
  {

    string header = string.Format(headerTemplate,"file"+i,files[i]);

    byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

    memStream.Write(headerbytes,0,headerbytes.Length);
    length += headerbytes.Length;

    FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read);
    byte[] buffer = new byte[1024];

    int bytesRead = 0;

    while ( (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 )
    {
       memStream.Write(buffer, 0, bytesRead);
       length += bytesRead;
    }


   memStream.Write(boundarybytes,0,boundarybytes.Leng th);
   length += boundarybytes.Length;

   fileStream.Close();
   }

   httpWebRequest2.ContentLength = memStream.Length;

   Stream requestStream = httpWebRequest2.GetRequestStream();

   memStream.Position = 0;
   byte[] tempBuffer = new byte[memStream.Length];
   memStream.Read(tempBuffer,0,tempBuffer.Length);
   memStream.Close();
   requestStream.Write(tempBuffer,0,tempBuffer.Length);
   requestStream.Close();

   WebResponse webResponse2 = httpWebRequest2.GetResponse();

   Stream stream2 = webResponse2.GetResponseStream();
   StreamReader reader2 = new StreamReader(stream2);


   MessageBox.Show(reader2.ReadToEnd());

   webResponse2.Close();
   httpWebRequest2 = null;
   webResponse2 = null;
}

 

 

 


评论 [发表评论]
账号 密码 还没帐号呢,现在注册一个?

免责声明:好库网所展示的信息由买卖双方自行提供,其真实性、准确性和合法性由信息发布人负责。好库网不提供任何保证,并不承担任何法律责任。