Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What I’m trying to do is…

Let the user select a PDF file and some other files using a WCF…once they select the file…those files need to be moved from there to a remote server (company hosted). I’m trying to deserialize the data (reading it as bytes) and transferring it over. I created a Form just as a testing purpose and to see how my client is going to work.

When I get the file....I can display the file but I want to get the actual PDF back and I’m not being able to do that. I can open the file in notepad (but its in the byte format) and when I try to open it in PDF it says that the file format is not supported. I’m really confused and don’t know what needs to be done.

Your help will be really appreciated.

Code Snippet:

Client Side:

    private void btnUploadFile_Click(object sender, EventArgs e)
            {
            string pathServer = @"C:\Users\....\Desktop\Test.pdf";

            RestClient newClient = new      RestClient("http://localhost:...../Service1.svc/DisplayRawData");
            var request = new RestRequest(Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
            IRestResponse<TempString> newResponse = newClient.Execute<TempString>(request);
            //List<TempString> rtrn = (List<TempString>)newResponse.Data;     
            var responseData = newClient.DownloadData(request);

            FileStream fStream = new FileStream(pathServer, FileMode.Create);

            BinaryWriter bw = new BinaryWriter(fStream);

            bw.Write(responseData);
            bw.Close();

            foreach (var xbyte in responseData)
            {
               // fStream.WriteByte(xbyte); 

            }

            //fStream.Flush();
            fStream.Close(); 

Server Side (Service)

public string DisplayRawData()
        {
            string path = @"C:\basketball.pdf";
            byte[] fileToSend = File.ReadAllBytes(path);
            string filetoSendB64 = Convert.ToBase64String(fileToSend);
           // WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";

            return filetoSendB64;
        }

Interface

        //Getting Stream from a File
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "DisplayRawData",
                    RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

        // string DisplayRawData();

        string DisplayRawData();
share|improve this question
    
The code doesn't seem to match what you are saying you want to do.. –  Tim B Jun 10 '13 at 19:19
    
could you please let me know where i'm doing wrong and where it does not match? –  Arka Jun 10 '13 at 19:23

1 Answer 1

The code is a bit confusing, but I would say it looks like you need to decode the response string from Base64 back to a byte array before writing it to a file.

share|improve this answer
    
Thanks Tim...thats what i thought as well...but how do i decode it so that i can write it back as a pdf....and when i do RestClient.Downloaddata it actually puts it into a byte array –  Arka Jun 10 '13 at 19:28
    
I'm not familiar with the library that you are using. Is it RestSharp? Try using DownloadString instead if there is a method called that. Or you could change your service to return binary data instead of JSON. –  Tim B Jun 10 '13 at 19:32
    
yep...i'm using RestSharp –  Arka Jun 10 '13 at 19:35
    
there isn't a downloadstring call in RestSharp –  Arka Jun 10 '13 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.