0
Answer

Pass file to servlet from c#.net application

hofsoc

hofsoc

15y
4.5k
1
I want to pass a file from my c#.net application and I am using WebClient.UploadFile(). I am not sure if this is the best way of doing this. At the moment I cannot access the file content within the servlet -  Can anyone help me here?

I have tried to pass the file source (xml) as a String within a nameValueCollection item, however, it seems that it is too long to be added to the collection. As u can see I have also tried to simply upload the String...

Any help would be appreciated...

This is what I have so far....

//upload the file
                WebClient client = new WebClient();


                //create a collection of values
                NameValueCollection nvc;

                String fileSource = getFileSource(file);

                Console.WriteLine("File Source Is: " + fileSource);

                nvc = setCollectionValues(m1, securityToken, file);


                try
                {
                    //upload the file to the servlet
                    //client.UploadFile(url, "post", file);
                    //client.UploadString(url, fileSource);

                   
                    //add the header to the request
                    client.Headers.Add(nvc);



                }
                catch
                {
                    //log an error here and complete proper exception handling
                }

                Stream data;
                StreamReader reader;
                String responseFromServer;


                data = client.OpenRead(url);

                reader = new StreamReader(data);

                responseFromServer = reader.ReadToEnd();

                //clean up the streams
                reader.Close();
                data.Close();

}

private static string getFileSource(String fileName)
        {

            FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            StreamReader reader = new StreamReader(file);

            String s = reader.ReadToEnd();

            reader.Close();
            file.Close();


            return s;
        }

Next Recommended Forum