Hi ,
I have a Wcf Service who has method to store image passed at configurable path.
Here is my WCf Service interface :
[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";
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()))
{
var fileByteArray = File.ReadAllText(fileStreamForUpload[0].DirectoryName).ToCharArray();
streamWriter.Write(fileByteArray, 0, fileByteArray.Length);
streamWriter.Close();
}
}
catch (Exception)
{
throw;
}
}
fileStreamForUpload is a list of FileInfo.
Error in call back [getStreamCallBack] :
System.ArgumentException: Value does not fall within the expected range.