Uploading Product Images
Multiple images can be uploaded for a product by a single multipart form data POST request. Binary file data and additional (product) data are multipart MIME formatted.
POST http://localhost:1260/api/v1/Uploads/ProductImages Content-Type: multipart/form-data; boundary=----------2a7fbadf8e7d4276b4a5fdbb9f877e11 ------------2a7fbadf8e7d4276b4a5fdbb9f877e11 Content-Disposition: form-data; name="Id" 1 ------------2a7fbadf8e7d4276b4a5fdbb9f877e11 Content-Disposition: form-data; name="my-image1"; filename="iphone-white.png" Content-Type: image/png <Binary image data here (length 74290 bytes)...> ------------2a7fbadf8e7d4276b4a5fdbb9f877e11 Content-Disposition: form-data; name="my-image2"; filename="iphone-black.jpg" Content-Type: image/jpeg <Binary image data here (length 269008 bytes)...> ------------2a7fbadf8e7d4276b4a5fdbb9f877e11--
First, you have to specify a product identifier to identify the product entity with which the images should be associated. There are three types of identifiers:
- Id: The SmartStore.NET internal, numeric product record identifier.
- Sku: The stock keeping unit of the product.
- Gtin: A global trade item number like UPC (in North America), EAN (in Europe), JAN (in Japan) and ISBN (for books).
Use one of these types and a proper value to identify the product. In the example, Id
is used with a value of 1.
It doesn't matter if one of the uploaded images already exists. The Web API automatically ensures that a product has no duplicate images by comparing both binary data streams.
It's also possible to update/replace an existing image. To do so simply add the picture identifier as PictureId
attribute in the content disposition header. The example updates the picture entity with the Id 6166.
POST http://localhost:1260/api/v1/Uploads/ProductImages Content-Type: multipart/form-data; boundary=----------34af7bc6762c4cbeadf3c2a8815c5b44 ------------34af7bc6762c4cbeadf3c2a8815c5b44 Content-Disposition: form-data; name="Id" 1 ------------34af7bc6762c4cbeadf3c2a8815c5b44 Content-Disposition: form-data; name="my picture"; filename="my-picture-file.jpg"; PictureId="6166"; CustomValue1="d8d096446c354cf781b0c6dfb46933fa"; CustomValue2="hello world" Content-Type: image/jpeg <Binary file data here (length 1590347 bytes)...> ------------34af7bc6762c4cbeadf3c2a8815c5b44--
The response is always a JSON formatted composite model with the following properties:
- Name: The name attribute of the content-disposition multipart header.
- Filename: The filename attribute of the content-disposition multipart header.
- MediaType: The media (mime) type of the content-type multipart header.
- Exists: If the uploaded image already exists, it is ignored.
- Inserted: Indicates whether the uploaded image has been inserted.
- ImageUrl: The Url of the default size image.
- ThumbImageUrl: The Url of the thumbnail image.
- FullSizeImageUrl: The Url of the full size image.
- ContentDisposition: Raw custom parameters of the content-disposition multipart header.
- PictureId: The picture identifier.
- Picture: The picture entity. Can be blank.
Image uploading can be a resource-intensive process. We recommend the use of the async and await syntax (.NET framework 4.5), a continuation task (.NET framework 4.0) or any other parallel asynchronous mechanism targeting payload efficiency.