简体   繁体   English

Prestashop-用C#上传图像Web服务

[英]Prestashop - Upload image web service in c#

I'm developing an app for a company. 我正在为一家公司开发应用程序。 So, for the app they want to upload images to prestashop. 因此,对于该应用程序,他们希望将图像上传到prestashop。 The problem basically is that I can't manage to do it through web service. 基本上,问题是我无法通过Web服务来做到这一点。 I always get an error 66: 我总是收到错误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). 我已经尝试了一切(邮递员,httpclient,webclient)。 And the only thing that worked was the library of Prestasharp. 唯一起作用的是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. 好吧,我终于用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. 我被卡住的部分是AddFileBytes方法。

Hope it helps somebody! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM