Prestashop - Unable to upload a new image product with the prestashop 1.6 webservice -


i'm trying upload new image product prestashop webservice through vb .net application, following error message:

"unable save image".

the url used upload image this: http://localhost/prestashop/api/images/products/1 , source code of function make request this:

public sub executeaddimage(byval resource string, byval id string, byval imagetoadd image)     dim response string = nothing      try         dim ms new memorystream()         imagetoadd.save(ms, system.drawing.imaging.imageformat.jpeg)         dim bytearray byte() = ms.toarray()          dim requesturl string = me.webserviceurl & "/" & resource & "/" & id         msgbox(requesturl)         dim webrequest httpwebrequest = directcast(system.net.webrequest.create(requesturl), httpwebrequest)         webrequest.method = webserviceprestashop.crudmethod.create         'webrequest.contenttype = "image/jpeg"         webrequest.contenttype = "application/x-www-form-urlencoded"         webrequest.credentials = new networkcredential(me.loginname, webserviceprestashop._password)         webrequest.contentlength = bytearray.length         msgbox(bytearray.length)         ' request stream         using datastream stream = webrequest.getrequeststream()             datastream.write(bytearray, 0, bytearray.length)         end using          ' response         using webresponse httpwebresponse = directcast(webrequest.getresponse(), httpwebresponse)             if webresponse.statuscode = httpstatuscode.ok                 using reader new streamreader(webresponse.getresponsestream(), encoding.utf8)                     dim imagenew image = image.fromstream(webresponse.getresponsestream())                 end using             end if         end using      catch ex webexception         msgbox(ex.message.tostring())         dim reader new streamreader(ex.response.getresponsestream)         msgbox(reader.readtoend)     end try end sub 

i'm using http post method, , post content bynary content of new image.

how can fix it?.

here solution. think key must write body of webrequest programatically adding stream of webrequest boundary (in binary array format), content-type chain (in binary array format) , content of image upload (in binary array format).

public sub executeaddimage(byval resource string, byval id string, byval imagetoadd byte())     dim response string = nothing       try         dim requesturl string = "urlshop" & "/api/" & resource & "/" & id         msgbox(requesturl)         dim webrequest httpwebrequest = directcast(system.net.webrequest.create(requesturl), httpwebrequest)         webrequest.keepalive = true         webrequest.credentials = new networkcredential(me.loginname, webserviceprestashop._password)         webrequest.contentlength = imagetoadd.length         webrequest.method = "post"         webrequest.contenttype = "image/jpeg"           dim boundary string = "----" & datetime.now.ticks.tostring("x", cultureinfo.invariantculture)         webrequest.contenttype = "multipart/form-data; boundary=" & boundary         dim beginpostdata = "--" & boundary & vbcrlf & "content-disposition: form-data; name=""image""; filename=""torrente.jpg""" & _             vbcrlf & "content-type: image/jpeg" & vbcrlf & vbcrlf         dim boundarybytes = system.text.encoding.ascii.getbytes(vbcrlf & "--" & boundary & "--" & vbcrlf)         dim beginpostdatabytes = system.text.encoding.ascii.getbytes(beginpostdata)           webrequest.contentlength = beginpostdata.length + imagetoadd.length + boundarybytes.length           ' request stream         using datastream stream = webrequest.getrequeststream()             datastream.write(beginpostdatabytes, 0, beginpostdatabytes.length)             datastream.write(imagetoadd, 0, imagetoadd.length)             datastream.write(boundarybytes, 0, boundarybytes.length)         end using           ' response         using webresponse httpwebresponse = directcast(webrequest.getresponse(), httpwebresponse)             if webresponse.statuscode = httpstatuscode.ok                 using reader new streamreader(webresponse.getresponsestream())                     response = reader.readtoend()                     msgbox(response)                 end using             end if         end using       catch ex webexception         msgbox(ex.message.tostring())         dim reader new streamreader(ex.response.getresponsestream)         msgbox(reader.readtoend)     end try end sub 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -