Prestashop - 无法使用prestashop 1.6 webservice上传新的图像产品

时间:2014-09-09 20:25:06

标签: prestashop prestashop-1.6

我正在尝试使用prestashop webservice通过vb .net应用程序上传产品的新图像,但是我收到以下错误消息:

“无法保存此图片”。

用于上传图片的网址为:http://localhost/prestashop/api/images/products/1 并且发出请求的函数的源代码是:

Public Sub executeAddImage(ByVal resource As String, ByVal id As String, ByVal imageToAdd As Image)
    Dim response As String = Nothing

    Try
        Dim ms As New MemoryStream()
        imageToAdd.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim byteArray As Byte() = ms.ToArray()

        Dim requestUrl As String = Me.WebServiceURL & "/" & resource & "/" & id
        MsgBox(requestUrl)
        Dim webRequest As 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)
        ' Get the request stream
        Using dataStream As Stream = webRequest.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
        End Using

        ' Get the response
        Using webResponse As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
            If webResponse.StatusCode = HttpStatusCode.OK Then
                Using reader As New StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)
                    Dim imageNew As Image = Image.FromStream(webResponse.GetResponseStream())
                End Using
            End If
        End Using

    Catch ex As WebException
        MsgBox(ex.Message.ToString())
        Dim reader As New StreamReader(ex.Response.GetResponseStream)
        MsgBox(reader.ReadToEnd)
    End Try
End Sub

我正在使用HTTP POST方法,POST内容是新图像的旁路内容。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

这里的解决方案。 我认为关键是我必须编写webrequest的主体,以编程方式添加到webrequest的流边界(以二进制数组格式),Content-Type链(以二进制数组格式)和要上载的图像的内容(二进制数组格式)。

Public Sub executeAddImage(ByVal resource As String, ByVal id As String, ByVal imageToAdd As Byte())
    Dim response As String = Nothing


    Try
        Dim requestUrl As String = "urlShop" & "/api/" & resource & "/" & id
        MsgBox(requestUrl)
        Dim webRequest As 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 As 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


        ' Get the request stream
        Using dataStream As Stream = webRequest.GetRequestStream()
            dataStream.Write(beginPostDataBytes, 0, beginPostDataBytes.Length)
            dataStream.Write(imageToAdd, 0, imageToAdd.Length)
            dataStream.Write(boundaryBytes, 0, boundaryBytes.Length)
        End Using


        ' Get the response
        Using webResponse As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
            If webResponse.StatusCode = HttpStatusCode.OK Then
                Using reader As New StreamReader(webResponse.GetResponseStream())
                    response = reader.ReadToEnd()
                    MsgBox(response)
                End Using
            End If
        End Using


    Catch ex As WebException
        MsgBox(ex.Message.ToString())
        Dim reader As New StreamReader(ex.Response.GetResponseStream)
        MsgBox(reader.ReadToEnd)
    End Try
End Sub