Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello developers,

I m working on the site where i m uploading a image with name and that uploaded file need to be store in another site. e.g. i m uploading image in www.abc.com that needs to be store in www.xyz.com file system. In short its a cross domain issue i m working on.
i used a webservice with querysting parameter for achieving this but querysting has limitation so whenever i send image bytearray then its not able to send the whole image because of its limitation.
i need another way to solve this proble, plz suggest me the way to solve the problem.

one important thing here i cant add webreference because the same solution have multiple output instatnces so i m using a webservice without any reference added in my solution. Another thing dont suggest me to use ftp client call because it takes creadential that i m not allowed to use.

i need a webservice method structure that accepts two parameter and the code that call that webmethod and pass these two parameters without any referece added.


plz help me.

my code is :
C#
[webmethod]

public int StoreImage()
        {
            try
            {
                if (System.Web.HttpContext.Current.Request.QueryString["filename"] != null && System.Web.HttpContext.Current.Request.QueryString["imgByte"] != null)
                {
                    Byte[] imgbyte = Encoding.ASCII.GetBytes(System.Web.HttpContext.Current.Request.QueryString["imgByte"].ToString());
                    System.IO.File.WriteAllBytes(Server.MapPath("\\images\\BannerImages\\") + System.Web.HttpContext.Current.Request.QueryString["filename"].ToString(), imgbyte);
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

[page code that call my webmethod]

string filename = fileuploader1.FileName;
            Byte[] imgByte = null;
            if (fileuploader1.HasFile && fileuploader1.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = fileuploader1.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
            }

            string ServeurURL = "http://xyz/SaveBannerImages.asmx/StoreImage?filename=" + filename + "&imgByte=" + imgByte;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ServeurURL);
            request.ContentType = "text/xml";
            request.Method = "POST";

            using (System.IO.Stream stream = request.GetRequestStream())
            {
                byte[] arrBytes = imgByte;
                stream.Write(arrBytes, 0, arrBytes.Length);
            }

            // Get the response
            WebResponse response = null;
            try
            {
                response = request.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader rdr = new StreamReader(stream, System.Text.Encoding.UTF8))
                    {
                        // Deserialize the response
                        string strResponse = rdr.ReadToEnd();
                        idmsg.Text = strResponse;
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }


above code is working but in querysting i cant send image byte array. so plz suggest me the efficient way to do this.
Posted
Updated 19-Apr-17 3:26am
v3

I think basic problem you have here is that you want to use web service but you don't want to add web reference that is quite right.Well I will never recommend any body to consume web service by adding web reference rather in professional word for consuming web service you need to follow below mention steps.

1. Generate a Proxy for the Web Service using WSDL.exe (Web Services Description Language Tool [^])

2. Add the proxy file to your project where you want to consume the web service.

3.Keep the url of web service in web.config/app.config and pass it as parameter to proxy class constructor. So that you can change the location of web service with out making code change.

Below link can give you better understanding

Building Web Service Clients using the .NET Framework[^]

For uploading image you can check below links

upload image to web service using c#[^]

Upload any type of File through a C# Web Service[^]
 
Share this answer
 
Comments
Member 10359492 26-Oct-13 2:28am    
thank you for spending your valuable time to help me out. but as i clearly mention in my post i cant use proxy class/ web reference in my solution. See, i m developing a admin panel which handles the contents and documents for other 6 websites. so whenever i upload a image/document via admin panel that image/document need to save in the file system of respective site which i choose to upload from my admin panel. so as per my knowledge its not a good idea to add web reference or proxy for each website in a single solution (admin panel). that's why i need a solution in which i can dynamically choose a web service to work. hope you understand my problem and vll help me soon. thank u once again.
Mahesh Bailwal 26-Oct-13 2:52am    
well in that case you can dynamically change the url of web service depending on which website you want to upload the file.
As I mentioned above that you can pass web service url as parameter to the proxy class constructor.
Member 10359492 26-Oct-13 7:10am    
thank you sir for helping me it worked
Mahesh Bailwal 26-Oct-13 8:43am    
your welcome :) can you please mark this question solved
As you probably noted, even Visual Studio has a built-in feature to consume any kind of web service. It is straightforward, that there is no webreference defined for them. The technology you have to consider is WSDL. So you either create the proxy statically as Mahesh Bailwal suggested (see this tutorial: http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2010/05/14/how-to-call-invoke-a-web-service-without-adding-web-reference.aspx[^]), or you go even beyond, and get the framework dynamically create the proxy on runtime (see: Call WebService Dynamically and Read WebService Definition[^]). This later method is useful when you have to be able to consume services unknown (or not properly known) on design time.
 
Share this answer
 
Comments
Member 10359492 26-Oct-13 2:29am    
thank you for spending your valuable time to help me out. but as i clearly mention in my post i cant use proxy class/ web reference in my solution. See, i m developing a admin panel which handles the contents and documents for other 6 websites. so whenever i upload a image/document via admin panel that image/document need to save in the file system of respective site which i choose to upload from my admin panel. so as per my knowledge its not a good idea to add web reference or proxy for each website in a single solution (admin panel). that's why i need a solution in which i can dynamically choose a web service to work. hope you understand my problem and vll help me soon. thank u once again.
Zoltán Zörgő 26-Oct-13 3:57am    
Don't copy-paste comments, read the answer properly. As you could see on the second link I have posted, there is no hardcoded reference or proxy. It is made at runtime. By the way calling a webservice is just a http transaction. So it can be imitated on a lower level if needed. An other technology you should be awaew about is UDDI.
Member 10359492 26-Oct-13 7:10am    
thank you so much sir it worked. thank you once again...

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