Upload an image byte by byte while reading the stream - wcf

i'm not sure if the title is understandable or not. But all i want to ask is, i want to upload a jpeg file from my WindowsPhone 8 client app. to a WCF web service. But i dont want direct upload, i want to handle the stream.So i wrote a script for reading image. Here it is:`
byte[] buffer = new byte[32*1024];
int read;
using (MemoryStream ms=new MemoryStream())
{
while ((read = Fs.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}`
Fs is myImage's reader.( FileStream Fs = new FileStream("FİLE", FileMode.Open, FileAccess.Read); )
For now everthing seems ok. But while i'm reading the image file i want to upload byte array of it. And server-side Wcf service will join the array and after joining i'll push it to my Azure Storage account(blob).
You can say: You dont need to this. Use Phone.Storage and push it to your blob. But i want to show progressing level of uploading.
ms.Write(buffer, 0, read);
after this , Wcf method have to work but how? How can i handle this situation? if it is possible and want to write this Wcf with async methods.
Thanks for your answers and sorry about my bad expression.

Well, here's a sample about the WCF part. Let me know if you need something else.
<system.serviceModel>
…
<bindings>
<basicHttpBinding>
<binding name="ExampleBinding" transferMode="Streaming"/>
</basicHttpBinding>
</bindings>
…
<system.serviceModel>
[ServiceContract]
public interface IStreamedService
{
[OperationContract]
void Upload(Stream data);
}

Related

How to send canvas image large size from silverlight to WCF service

I have a large size byte[] formed from silverlight Canvas using following code
var img = new WriteableBitmap(cnvControlHolder, null);
var outStream = new MemoryStream();
EncodeJpeg(img, outStream);
Now I want to send this to WCF service to form image from this byte array & save it as an image on server side so that I can consume it in SSRS. My problem is as the byte[] is big I get the classic Method not found from WCF service.
I read in few links that WCF streaming would be one option, but could not find any sample on the net. My service method is like this:
public bool Upload(Stream image)
{
FileStream fileStream = null;
BinaryWriter writer = null;
var filePath = HttpContext.Current.Server.MapPath(".") + #"\" +
ConfigurationManager.AppSettings["PictureUploadDirectory"] + #"\Diagram.jpeg";// +image.ImageName;
if (image!=null)
{
//return ByteArrayToFile(filePath, image.Imagestream);
fileStream = File.Open(filePath, FileMode.Create);
writer = new BinaryWriter(fileStream);
writer.Write("Diagram.jpeg");
}
return false;
}
and client call is this :
var img = new WriteableBitmap(canvas1, null);
var outStream = new MemoryStream();
EncodeJpeg(img, outStream); //custom library to compress into jpeg
var client = new Service1Client();
client.UploadCompleted += new EventHandler<UploadCompletedEventArgs>(client_UploadCompleted);
client.UploadAsync(outStream.ToArray());
Can somebody suggest some sample or any other solution to fix my issue.
I recently implemented a very similar solution in Silverlight. The solution involves:
Dividing the large byte[] into n chunks of size that can be sent via a web service call
Making a web call to the service, registering a file upload request for n chunks, and requesting a guid from the service.
Making n web calls to the service and uploading each chunk, supplying the guid and the ordinal of the chunk (the chunks may arrive out of sequence).
Once the server receives all n chunks, it combines the chunks and writes the data into a file.
I hope this can help to get you started.

Restful WCF Service - returning byte[]?

I'm working with a RESTful WCF service. One of the service methods returns byte[] (which contains a file).
On the client side, I have some code that uses the WebRequest class to invoke that particular service method. Using the WebRequest, I'm getting the response stream like:
Stream stream = webReq.GetResponse().GetResponseStream();
From this stream, I am then reconstructing a byte[] and then outputting the File locally. The problem is that the reconstructed file on the client-side doesn't resemble the file that was returned from the service side (I get a corrupt PDF file that is much larger in size than the one sent from the service side). Just before the service method returns the byte[], I outputted that byte[] to disk on the service side and it created the file fine... This points to something going wrong between that method returning the byte[] and my client side reconstructing the byte[] from a Stream on the client side... to reconstruct the byte[] from the Stream, I'm using the following method which someone posted in the past on stackoverflow:
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();
}
}
Any ideas what could be going wrong?
I guess that the response from the server contains some envelope in addition to the raw bytes. Like a XML envelope or something. Which would of course suppose that the bytes are base64 encoded string in the response because you cannot store binary data into XML. It would also explain why you are getting a bigger buffer on the client than the actual PDF that the server has sent.
This will of course depend on what binding your WCF service uses and how is it configured. When you dump the contents of the MemoryStream you are reading on the client what exactly do you see? This should give you further hints on how the actual PDF file is encoded in the HTTP response body.

WCF Streaming: Last byte is lost

I have a WCF service which has one method returning a stream.
[ServiceContract]
public interface IService1
{
[OperationContract]
MyMessage Test();
}
Now, MyMessage is defined like this:
[MessageContract]
public class MyMessage
{
public MyMessage(string file)
{
this.Stream = File.OpenRead(file);
this.Length = Stream.Length;
}
[MessageHeader]
public long Length;
[MessageBodyMember]
public Stream Stream;
}
Peachy.
The service has a streamed response, using basicHttpBinding. This is the binding configuration:
<basicHttpBinding>
<binding name="BasicStreaming"
maxReceivedMessageSize="67108864" maxBufferSize="65536" transferMode="StreamedResponse" />
</basicHttpBinding>
Now this is where things start to get interesting. When calling this service, the last byte is lost if i read the stream in a particular way. Here is the code illustrating the two different approaches:
Service1Client client = new Service1Client();
//this way the last byte is lost
Stream stream1;
var length = client.Test(out stream1);
var buffer1 = new byte[length];
stream1.Read(buffer1, 0, (int)length);
File.WriteAllBytes("test1.txt", buffer1);
stream1.Close();
//here i receive all bytes
Stream stream2;
length = client.Test(out stream2);
var buffer2 = new byte[length];
int c = 0, b;
while ((b = stream2.ReadByte()) != -1)
{
buffer2[c++] = (byte)b;
}
File.WriteAllBytes("test2.txt", buffer2);
stream2.Close();
I am sure I'm missing something, but can anyone point out to me exactly why this is happening? The biggest problem is that in another service, whichever way i read the stream, i lose the last byte, but maybe by identifying the problem here I can solve that one too.
Technical details:
IIS 7.0
.NET 3.5
Basic HTTP Binding
Streamed response mode
Note: I have uploaded the project isolating the problem, so anyone can try it out: mediafire
I don't have an exact answer as to why this works (my brain isn't fully engaged at the moment), however this DOES work:
var buffer1 = new byte[length+2];
stream1.Read(buffer1, 0, buffer1.Length);
(and, yes, you end up with a buffer that's too large. It's just a starting point for further thinking)
In testing I found +1 isn't large enough, but +2 is.
Why passing a count of (length + 1)? It should be length, otherwise you are attempting to read one more byte than what is available.

WCF 4.0 REST Upload MS-Excel File

I am trying to upload MS-Excel file through WCF-REST Service.
I used the solution given in below post:-
RESTful WCF service image upload problem
My POST Method is declared as:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/RFQ")]
[WebContentType("application/octet-stream")]
void UploadRFQDoc(Stream fileContents);
When I debug, stream content is fine till the call goes, and when I attach service to debug, Stream fileContents parameter becomes null , and service returns with [Bad Request]. I am not sending large file (it is just 50 KB). I am using HttpClient to call the Post.
Here are the client code(RestClient is HttpClient).
protected void Post(string uri, Stream stream, int length)
{
var content = HttpContent.Create(output => CopyToStream(stream, output, length), "application/octet-stream", length);
Uri relativeUri = new Uri(uri, UriKind.Relative);
var resp = RestClient.Post(relativeUri, content);
ProcessResponse(resp);
}
void CopyToStream(Stream input, Stream output, int length)
{
var buffer = new byte[length];
var read = input.Read(buffer, 0, Convert.ToInt32 (length));
output.Write(buffer, 0, read);
}
Any clue what else can go wrong.
Many Thanks.
[WebContentType("application/octet-stream")] attribute was unnecessary here. I commented it out, and all worked fine :).

Silverlight changes the io.Stream to byte[]

I have created a WCF service for uploading images , which accepts System.IO.Stream as input parameter and am using streaming. When I added the service reference in Silverlight project then it automatically changed the parameter of my WCF method from System.IO.Stream to byte[]. Can anyone suggest if there is a way around this so that I can get System.IO.Stream type rather than byte[].
Thanks in advance
Silverlight does not support transfer mode streamed: http://forums.silverlight.net/forums/t/119340.aspx
So I think that you are stuck with getting a byte array.
Can you verify that you're not hitting one of the reader quotas in the service? You can try increasing all of them to see if this solves your problem.
I think you should set the transferMode property of your basicHttpBinding to the correct value, as described in this article. And then add the service reference to your Silverlight application again.
http://blogs.msdn.com/b/carlosfigueira/archive/2010/07/08/using-transfermode-streamedresponse-to-download-files-in-silverlight-4.aspx
Even I was struggling with the same issue. At last I got a solution by myself. All you can do is:
declare the accepting parameter as string array in the WCF Service.
convert the byte array into string array at client place.
After Sending the converted byte array as string array again convert back it into byte array.
eg. at the WCF side:
[DataContract]
Class FileInfo
{
[DataMember]
string filename;
[DataMember]
string[] StrArr;
}
the receiving function:
public void uploadFile(FileInfo fi)
{
int len=fi.StrArr.len;
byte[] myFileByte=new byte[len];
for(int i=0;i<len;i++)
{
myFileByte[i]=Convert.ToByte(fi.StrArr[i]);
}
//your uploaded File buffer is ready as myFileByte
//proceeding operations are most welcome here......
.........
}
At Client Side:
public void UploadMyFile()
{
//Take the InputStream from the selected File as iStream;
int len=(int)iStream.length;
byte[] buffer=new byte[len];
string[] MyStrArr=new string[len];
for(int i=0;i<len;i++)
{
MyStrArr[i]=Convert.ToString(buffer[i]);
}
//Here your string array is ready to send to the WCF Service....
//I m confident this code will work perfectly with some file limitation consideartions.
}