Click here to Skip to main content
15,070,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
I have a Wcf Service who has method to store image passed at configurable path.

Here is my WCf Service interface :

C#
[ServiceContract]
public interface IFileUploader
{
   [OperationContract]
   [WebInvoke(Method = "POST", UriTemplate = "FileUpload/{fileName}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
       void SaveFile(string fileName, Stream file);
}


i have implemented it , using some custome class lib.

At Client side :

i tried using Web Client but has not came cross image/file uploading.
and used HttpWebClient as well here is the code :
public void SomeMethod()
{
   HttpWebRequest uploadReq = (HttpWebRequest)WebRequest.Create(fileUploadSvcBaseUri + "/FileUpload");
   uploadReq.Method = "POST";
   uploadReq.ContentType = "image/png";
   //var bytearr = new byte[fileStream.ReadByte(]
                
   uploadReq.BeginGetRequestStream(new AsyncCallback(getStreamCallBack), uploadReq);
   fileStreamForUpload.Add(dialpg.File);
}

public void getStreamCallBack(IAsyncResult asynchronousResult)
{
   try
   {
      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
      using (StreamWriter streamWriter = new StreamWriter(response.GetResponseStream()))
      {
         //string resultString = streamReader1.ReadToEnd();
         //strea
         //resultBlock.Text = "Using HttpWebRequest: " + resultString;
         var fileByteArray = File.ReadAllText(fileStreamForUpload[0].DirectoryName).ToCharArray();
         streamWriter.Write(fileByteArray, 0, fileByteArray.Length);
         streamWriter.Close();
      }
      //using (StreamWriter wrtr = new StreamWriter(request.))
   }
   catch (Exception)
   {
      throw;
   }
}


fileStreamForUpload is a list of FileInfo.
Error in call back [getStreamCallBack] :

System.ArgumentException: Value does not fall within the expected range.
Posted
Updated 20-Aug-14 4:41am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900