I'm developing an app for a company. So, for the app they want to upload images to prestashop. The problem basically is that I can't manage to do it through web service. I always get an error 66:
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code>
<![CDATA[66]]>
</code>
<message>
<![CDATA[Unable to save this image]]>
</message>
</error>
</errors>
</prestashop>
I've tried everything (postman, httpclient, webclient). And the only thing that worked was the library of Prestasharp. HOWEVER, my boss doesn't want to rely on external libraries for the app. (yeah, I don't get it either). Hence, I was wondering if someone could tell me how to upload the image without the library. For example, the following code does not work, but I think it's right.
string file = @"C:\Users\MyPC\Pictures\Example\50.jpg";
string webAddress = @"http://mywebsite.com/api/images/products/1?ws_key=NUZSHHQ1X456IJQDPXY3GUXG2C6AMAV3";
var client = new HttpClient();
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("image", file)
};
var content = new FormUrlEncodedContent(pairs);
MessageBox.Show( client.PostAsync(webAddress, content).Result.ReasonPhrase );
I've seen people who complain about the same thing, but no one has ever solved this.
I hope you guys can make it,
Kindest regards
Well, I've finally solved this with RestSharp.
public string uploadImage(string path, string id_product)
{
var client = new RestClient("http://mywebsite.com/api");
byte[] file = System.IO.File.ReadAllBytes(path);
var request = new RestRequest("images/products/" + id_product + "?ws_key=" + API_KEY, Method.POST);
request.AddFileBytes("image", file, Path.GetFileName(path));
IRestResponse response = client.Execute(request);
string content = response.Content;
return content;
}
The part where I was getting stuck was the AddFileBytes
method.
Hope it helps somebody!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.