Jump to content

Error 500 on Uploading Image via Webservice Vb.net


damgero

Recommended Posts

Im developing an interface that can manage products on Prestashop with data from an ERP, i have all the way to do it, but im getting issues on uploading images to products.. i follow this steps

 

1.- Create Product

2.- Get the Product ID

3.- Update Stocks

4.- Upload Image <- this is where im getting errors

 

i use the follow code to upload the image.. but it says error 500

 

i try sending image = null... prestashop api says.. that it cant upload that image, thats a good i think, i mean im sending the post rigth.

but when i choose a picture and loaded on image objet.. it trougths error 500.

 

PD:

i check that

store.page.com/api/images/product/XX exist

but only the products i manually upload the image via backoffice has that page.

the new products created by the interface does not had that page.

 

can anyone please give me a hand with this =)

 

 

 Try

           Dim CabeceraByte() As Byte
           Dim PieByte() As Byte
           Dim PostDatosByte() As Byte
           Dim PostDatos As String
           Dim CanaldeTransmision As Stream = Nothing
           Dim RequestURL As String = WebService_URL & "/" & TableName & "/" & Id
           Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(RequestURL), HttpWebRequest)
           webRequest.Method = CRUDMethod.Create
           webRequest.KeepAlive = True
           webRequest.Credentials = New NetworkCredential(WebService_LoginName, WebService_Password)
           Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture)
           webRequest.ContentType = "multipart/form-data; boundary=" & boundary

           CabeceraByte = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n")
           PieByte = Encoding.UTF8.GetBytes("\r\n" + boundary + "--\r\n")

           PostDatos = "\nContent-Disposition: form-data; name=" & Chr(34) & "image" & Chr(34) & "; filename=" & Chr(34) & "mysonda-" & Id & "-1.jpg" & Chr(34) & ";\r\nContent-Type: image/jpeg\r\n\r\n"
           PostDatosByte = Encoding.ASCII.GetBytes(PostDatos)

           webRequest.ContentLength = CabeceraByte.Length + PostDatosByte.Length + imagen.Length + PieByte.Length

           Try
               CanaldeTransmision = webRequest.GetRequestStream()
               CanaldeTransmision.Write(CabeceraByte, 0, CabeceraByte.Length)
               CanaldeTransmision.Write(PostDatosByte, 0, PostDatosByte.Length)
               CanaldeTransmision.Write(imagen, 0, imagen.Length)
               CanaldeTransmision.Write(PieByte, 0, PieByte.Length)
           Finally
               If Not CanaldeTransmision Is Nothing Then CanaldeTransmision.Close()
           End Try
           Dim response As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
           Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
           Dim strData As String = reader.ReadToEnd()
           reader.Close()
           Return strData
       Catch ex As WebException
           Dim ereader As StreamReader = New StreamReader(ex.Response.GetResponseStream())
           Dim strdata As String = ereader.ReadToEnd()
           ereader.Close()
           Return "X" & ex.Response.ToString & " " & strdata
       End Try

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...