gpt4 book ai didi

c# - Prestashop - 在 C# 中上传图像 Web 服务

转载 作者:行者123 更新时间:2023-11-30 21:42:17 24 4
gpt4 key购买 nike

我正在为一家公司开发应用程序。因此,对于应用程序,他们希望将图像上传到 prestashop。问题基本上是我无法通过网络服务来完成。我总是收到错误 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>

我已经尝试了一切( postman 、httpclient、webclient)。唯一有用的是 Prestasharp 的图书馆。但是,我的老板不想依赖应用程序的外部库。 (是的,我也不明白)。因此,我想知道是否有人可以告诉我如何在没有图书馆的情况下上传图片。比如下面的代码不行,但是我觉得是对的。

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 );

我见过有人提示同样的事情,但从来没有人解决过这个问题。

希望你们能成功,

致以诚挚的问候

最佳答案

好吧,我终于用 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;
}

我遇到困难的部分是 AddFileBytes 方法。

希望对大家有帮助!

关于c# - Prestashop - 在 C# 中上传图像 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42674115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com