2017-03-08 74 views
1

我正在爲一家公司開發應用程序。所以,對於他們想要將圖像上傳到prestashop的應用程序。這個問題基本上是我無法通過網絡服務來實現。我總是得到一個錯誤66:Prestashop - 在c上傳圖像網絡服務#

<?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> 

我試過一切(郵差,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); 

我見過有人抱怨同一件事,但沒有人解決過這個問題。

我希望你們能做到,

最親切的問候

回答

2

好了,我終於解決了這個與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方法。

希望它可以幫助別人!