2016-06-28 17 views
0

Wie mit Senden und Empfangen von Excel-Daten Web-ServiceWie zum Senden und Empfangen von Excel-Daten Web-Service

ich eine Web-Anwendung erstellt habe Excel-Datei Daten an einen Web-Service zu senden.

protected void Page_Load(object sender, EventArgs e) 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:58959/RESTServiceImpl.svc/PostFile"); 

     request.ContentType = "application/vnd.ms-excel"; 
     request.Headers.Add("filename", "fooBar.xls"); 
     request.Method = WebRequestMethods.Http.Post; 

     byte[] fileData = System.IO.File.ReadAllBytes("C:\\Users\\Public\\Documents\\Forecast Pro TRAC\\Input\\Book1.xls"); 
     request.ContentLength = fileData.Length; 

     using (System.IO.BinaryWriter postStream = new System.IO.BinaryWriter(request.GetRequestStream())) 
     { 
      postStream.Write(fileData); 
      postStream.Flush(); 
      postStream.Close(); 
     } 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     System.Diagnostics.Debug.Assert(response.StatusCode == HttpStatusCode.OK); 

     string responseMessage = string.Empty; 
     using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream())) 
     { 
      responseMessage = sr.ReadToEnd(); 
     } 
     System.Diagnostics.Debug.WriteLine(responseMessage); 
    } 

Jetzt muss ich diese Excel-Daten von WCF-Anwendung erhalten. Also habe ich sowas wie unten Code gemacht. Ich erhalte insgesamt Bytes, die ich sende. Was sind genau die Dinge, die ich tun muss, um Excel-Daten zu senden und zu empfangen?

[ServiceContract] 
public interface IRESTServiceImpl 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Bare, 
     UriTemplate = "PostFile")] 
    string PostFile(); 
} 
public class RESTServiceImpl : IRESTServiceImpl 
{ 
    public string PostFile() 
    { 
     var httpRequest = HttpContext.Current.Request; 

     var bites = httpRequest.TotalBytes; 

     return httpRequest.FilePath; 
     } 
} 

Ich bin sehr neu in Web-Service und das ist meine erste Anwendung. Bitte hilf mir bitte. Vielen Dank im Voraus.

Antwort

1

ich die Lösung und hier gelöst haben, ist der Arbeitscode

public class RestServiceImpl : IRestServiceImpl 
{ 
    public string PostFileRest(Stream fileContents) 
    { 
     var httpRequest = HttpContext.Current.Request; 

     var filePath = "C:\\file.xls"; //excel filePath for local 

     var bites = httpRequest.TotalBytes; 

     //Convert stream to byte array 
     byte[] reqBytes = readRequest(fileContents, bites); 
     byte[] decodedReqBytes = HttpUtility.UrlDecodeToBytes(reqBytes); 

     string json = System.Text.Encoding.UTF8.GetString(reqBytes); 
     DataTable dt = JsonConvert.DeserializeObject<DataTable>(json); 

     MemoryStream stream = new MemoryStream(reqBytes); 
     FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write); 
     stream.WriteTo(file); 
     file.Close(); 
     stream.Close(); 

     return json ; 
    } 

    #region Convert Stream to byte array 
    private byte[] readRequest(Stream fileContents, int bites) 
    { 
     System.IO.MemoryStream memStream = new System.IO.MemoryStream(); 
     int BUFFER_SIZE = bites; 
     int iRead = 0; 
     int idx = 0; 
     Int64 iSize = 0; 
     memStream.SetLength(BUFFER_SIZE); 
     while (true) 
     { 
      byte[] reqBuffer = new byte[BUFFER_SIZE]; 
      try 
      { 
       iRead = fileContents.Read(reqBuffer, 0, BUFFER_SIZE); 
      } 
      catch (System.Exception e) 
      { 
       System.Diagnostics.Debug.WriteLine(e.Message); 
      } 

      if (iRead == 0) 
      { 
       break; 
      } 

      iSize += iRead; 
      memStream.SetLength(iSize); 
      memStream.Write(reqBuffer, 0, iRead); 
      idx += iRead; 
     } 

     byte[] content = memStream.ToArray(); 
     memStream.Close(); 
     return content; 
    } 
    #endregion 
} 

Die Interface-

[ServiceContract] 
public interface IRestServiceImpl 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "PostFileRest")] 
    string PostFileRest(Stream fileContents); 
} 
Verwandte Themen