Jump to content

upload photo


Recommended Posts

Hello,

someone have developed a class for  uploading product's photo in c# calling the webservice? My c# code

           


            string filedto = FotoDto.UrlFolder;

            string webAddress = FotoDto.UrlPrestashop.Replace("languages", "images/products") + FotoDto.Id_Product;
           
           var credentials = new NetworkCredential(key, "");

            var handler = new HttpClientHandler { Credentials = credentials };

            var client = new HttpClient(handler);

            var pairs = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("image", filedto)
            };

            var content = new FormUrlEncodedContent(pairs);

            content.Headers.ContentType.MediaType = @"multipart/form-data";

            var response = client.PostAsync(webAddress, content).Result;

            ResponseWebService.HttpStatus = response.StatusCode;
            ResponseWebService.Xml = response.ReasonPhrase;

            return ResponseWebService;

 

But prestashop give me an error "bad request".

tnx

Share this post


Link to post
Share on other sites

if i try to sending the request by postman

<?xml version="1.0" encoding="UTF-8"?>

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">

    <errors>

        <error>

            <code>

                <![CDATA[76]]>

            

            </code>

            <message>

                <![CDATA[Please set an "image" parameter with image data for value]]>

            

            </message>

        </error>

    </errors>

</prestashop>

Share this post


Link to post
Share on other sites

[SOLVED]

 

byte[] ImageData;

            string filedto = FotoDto.UrlFolder;

            //FotoDto.Id_Product

            ResponseWebService = new ResponseWebServiceDto();

            string webAddress = FotoDto.UrlPrestashop.Replace("languages", "images/products") + FotoDto.Id_Product + "?ws_key=" + key;

            ImageData = File.ReadAllBytes(filedto);

            using (var client = new HttpClient())
            {
            
                var requestContent = new MultipartFormDataContent();

                var imageContent = new ByteArrayContent(ImageData);

                imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");

                requestContent.Add(imageContent, "image", FotoDto.Name + ".jpg");

                var response = client.PostAsync(new Uri(webAddress), requestContent).Result;

                ResponseWebService.HttpStatus = response.StatusCode;

                ResponseWebService.Xml = response.ReasonPhrase;

            }
            
            return ResponseWebService;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More