2013-06-21 56 views
0

我想從Silverlight發佈文件到服務器。我有FileInfo myFile;。在服務器端,我需要這個文件在Request.Files集合中。從silverlight發佈文件

我的問題是如何將myFile從Silverlight發佈到服務器以便能夠在服務器端的Request.Files集合中訪問它?

謝謝。

+0

你可以嘗試的HttpClient和發佈文件 – vinod8812

+0

我試圖安裝[HttpClient的(http://nuget.org/packages/Microsoft。 Net.Http),但由於某些原因,我無法在安裝後添加項目引用。這是說圖書館不是在Silverlight中構建的,所以它不能添加對項目的引用。相反我找到了其他解請閱讀我的答案。 – Augis

回答

0

這是我用來發布文件流到服務器的代碼。

private void LayoutRoot_Drop(object sender, DragEventArgs e) 
     { 
      _dropEventOccured = "Yes"; 
      FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[]; 

      MessageBox.Show(files.Length.ToString()); 

      //MessageBox.Show("Dropped"); 

      foreach (FileInfo fi in files) 
      { 
       _fileRead = "Yes"; 
       _file = fi; 
       FileStream fs = _file.OpenRead(); 
       objByte = ReadFully(fs); 
       fs.Close(); 
       Post(); 

      } 
     } 

public static byte[] ReadFully(Stream input) 
     { 
      byte[] buffer = new byte[16 * 1024]; 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       int read; 
       while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
       { 
        ms.Write(buffer, 0, read); 
       } 
       return ms.ToArray(); 
      } 
     } 


private void Post() 
     { 
      HttpWebRequest request = null; 
      //request = HttpWebRequest.Create("http://localhost/ajax.aspx") as HttpWebRequest; 
      //request = HttpWebRequest.Create("http://localhost/ajax.aspx") as HttpWebRequest; 
      request = HttpWebRequest.Create(string.Format("http://localhost/ajax.aspx?name={0}",_file.Name)) as HttpWebRequest; 
      //request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; 
      request.ContentType = "multipart/form-data"; 
      request.Method = "POST"; // Doesn't work with "GET" 
      request.ContentLength = objByte.Length; 

      if (request.Method == "GET") 
      { 
       request.BeginGetResponse(this.GetRequestStreamCallback, request); 
      } 
      else 
      { 
       MessageBox.Show("Posting..."); 
       request.BeginGetRequestStream(this.GetRequestStreamCallback, request); 
      } 


     } 

     private void GetRequestStreamCallback(IAsyncResult result) 
     { 
      System.Threading.Thread.Sleep(1000); 
      HttpWebRequest request = (HttpWebRequest)result.AsyncState; 

      using (Stream stream = request.EndGetRequestStream(result)) 
      { 

       stream.Write(objByte, 0, objByte.Length); 
       stream.Close(); 
      } 
      request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 
      //HttpWebResponse response = (HttpWebResponse)result.EndGetResponse(asyncResult); 
     } 

     private void GetResponseCallback(IAsyncResult result) 
     { 
      try 
      { 
       HttpWebRequest request = (HttpWebRequest)result.AsyncState; 
       while (!request.HaveResponse) 
       { 
        System.Threading.Thread.Sleep(1000); 
       } 
       HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); 
       Console.WriteLine(response.StatusDescription); 
      } 
      catch (Exception ex) 
      { 

      } 
     } 

這是服務器端的代碼:

protected void Page_Load(object sender, EventArgs e) 
      { 
string saveTo = Server.MapPath("~/App_Data/") + Request.QueryString["name"]; 
       // create a write stream 
       FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.Write); 
       // write to the stream 
       ReadWriteStream(Request.InputStream, writeStream); 
      } 



      private void ReadWriteStream(Stream readStream, Stream writeStream) 
      { 
       int Length = 256; 
       Byte[] buffer = new Byte[Length]; 
       int bytesRead = readStream.Read(buffer, 0, Length); 
       // write the required bytes 
       while (bytesRead > 0) 
       { 
        writeStream.Write(buffer, 0, bytesRead); 
        bytesRead = readStream.Read(buffer, 0, Length); 
       } 
       readStream.Close(); 
       writeStream.Close(); 
      }